How to join a table on a column that has a period/decimal in one table but not the other - left-join

Trying to join two tables on a column referencing diagnoses. One table uses the actual diagnosis code (aka has decimal), but the other table uses the diagnosis code sans decimal/period. Ex: A43.21 v A4321 or 553.1 v 5531
LEFT JOIN DB_VIEW.Reference_Diagnosis_Table RDT
ON RDT.Diagnosis_Code = SANS.Diagnosis_Code
But obviously they aren't equal to eachother... is there a way to write this so it interprets the RDT.Diagnosis_Code without having it? (FYI I do not have the ability to alter the existing data stored within the warehouse)

Related

Snowflake invalid identifier when performin a join

I have been trying to do an outer join across two different tables in two different schemas. I am trying to filter out before from the table variants the character that are smaller than 4 and bigger than 5 digits. The join was not working with a simply where clause in the end, hence this decision.
The problem is if I do not put the quotes, Snowflake will say that I put invalid identifiers. However, when I run this with the quotes, it works but I get as values in the fields of the column raw.stitch_heroku.spree_variants.SKU only named as the column name, all across the table!
SELECT
analytics.dbt_lcasucci.product_category.product_description,
'raw.stitch_heroku.spree_variants.SKU'
FROM analytics.dbt_lcasucci.product_category
LEFT JOIN (
SELECT * FROM raw.stitch_heroku.spree_variants
WHERE LENGTH('raw.stitch_heroku.spree_variants.SKU')<=5
and LENGTH('raw.stitch_heroku.spree_variants.SKU')>=4
) ON 'analytics.dbt_lcasucci.product_category.product_id'
= 'raw.stitch_heroku.spree_variants.SKU'
Is there a way to work this around? I am confused and have not found this issue on forums yet!
thx in advance
firstly single quote define a string literal 'this is text' where as double quotes are table/column names "this_is_a_table_name"
add alias's to the tables makes the SQL more readable, and the duplicate length command can be reduced with a between, thus this should work better:
SELECT pc.product_description,
sp.SKU
FROM analytics.dbt_lcasucci.product_category AS PC
LEFT JOIN (
SELECT SKU
FROM raw.stitch_heroku.spree_variants
WHERE LENGTH(SKU) BETWEEN 4 AND 5
) AS sp
ON pc.product_id = sp.SKU;
So I reduced the sub-selects results as you only used sku from sp but given you are comparing product_id to sku as your example exists you don't need to join to sp.
the invalid indentifiers implies to me something is named incorrectly, the first step there is to check the tables exist and the columns are named as you expect and the type of the columns are the same for the JOIN x ON y clause via:
describe table analytics.dbt_lcasucci.product_category;
describe table raw.stitch_heroku.spree_variants;

Postgresql get references from a dictionary

I'm trying to build a request to get the data from a table, but some of those columns have foreign keys I would like to replace by the associated keyword in one request.
Basically there's
table A with column 1:PKA-ID and column 2:name.
table B with column 1:PKB-ID, column 2:FKA-ID, column 3:amount.
I want to get all the lines in table B but with all foreign keys replaced by the associated names in table A.
I started building a request with a subrequest + alias to get that, but ofc I have more than one result per subrequest, yet I can't find a way to link that subrequest to the ID of table B [might be exhausted, dumb or both] from the main request. I did something like that:
SELECT (SELECT "NAME" FROM A JOIN B ON ID = FKA-ID) AS name, amount FROM TABLEB;
it feels so simple of a request yet...
You don't need a join in the subselect.
SELECT pkb_id,
(SELECT name FROM a WHERE a.pka_id = b.fka_id),
amount
FROM b;
(See it live in SQL Fiddle).
The subselect query runs for each and every row of its parent select and has the parent row available from the context.
You can also use a simple join.
SELECT b.pkb_id, a.name, b.amount
FROM b, a
WHERE a.pka_id = b.fka_id;
Note that the join version puts less restrictions on the PostgreSQL query optimizer so in some cases the join version might work faster. (For example, in PostgreSQL 9.6 the join might utilize multiple CPU units, cf. Parallel Query).

Left joining right aligned field with left aligned field in Crystal

I have a report where I need to join two tables but the fields are defined differently between them and I cannot change the table schemas because its JDEdwards. So I have one field as 30 characters left aligned and another another as 12 characters right aligned. The values will always be under 12 characters. So the issue is not the difference in size but the alignment/padding. I have to do a left join though. Right now the report is just comparing in the data selection and causing an inner join. But I need to change it to left join to stop losing rows. Is there any way to do it?
What happens is when I create the link to do the left join in Database Expert, I end up getting the data from the joined table all blank due to it not finding any rows because of the padding difference.
Since you only need one value from the table to be joined you can instead create a single SQL Expression to get the value for you without the need to add any tables to the report itself via Database Expert. You'll be able to use any valid SQL so you can do pretty much whatever you want without being constrained by Crystal.
For example, say your report consists of Table_A with a foreign key that is 30-characters left-aligned and you're trying to join to Table_B to get at some field, but that key is 12 characters right-aligned. A simple example in Oracle would be something like this:
case when "Table_A"."ForeignKey" is null then null
else
(select Table_B.SomeFieldYouWant
from Table_B
where rpad(Table_B.PrimaryKey,30,'0')="Table_A"."ForeignKey"
end
It's important to note that in a SQL Expression, any fields referenced with double quotes means that it's referring back to tables/fields in your report instead of new tables in the subquery; this is how you relate the subquery back to your report's data.

postgres: left join by unknown table - column value

I know this is a stupid question, but I'm gotta ask away.
I'm in a situation where the db I'm working on doesn't have a "backwards" relation to another table.
Let's say I have three tables - let's call them type tables, rt, wt, and ret.
Each of those tables has a group_id.
Each group table has type which may contain rt, wt, or ret.
The normal procedure for selection is select from a type table and then left join group on group_id.
I'm in a situation where I need to select in the reverse order, but I don't know which table I'm to be selecting from at run time. All I have is the type.
Is there such a thing as selecting a table by a column value? such as
SELECT * FROM group LEFT JOIN `group.type` ON `group.type`.group_id = group.id
In my case, I just did multiple left joins, of course only one of them stuck.

differing column names in self-outer-joins

When writing a self-join in tSQL I can avoid duplicate column names thus:
SELECT FirstEvent.Title AS FirstTitle, SecondEvent.Title AS FirstTitle
FROM ContiguatedEvents AS FirstEvent
LEFT OUTER JOIN ContiguatedEvents AS SecondEvent
ON FirstEvent.logID = SecondEvent.logID
Suppose I want to select all the columns from the self-join, for example into a view. How do I then differentiate the column names without writing each one out in the join statement. I.e. is there anything I can write like this (ish)
SELECT FirstEvent.* AS ???, SecondEvent.* AS ???
FROM ContiguatedEvents AS FirstEvent
LEFT OUTER JOIN ContiguatedEvents AS SecondEvent
ON FirstEvent.logID = SecondEvent.logID
There's no way to automatically introduce aliases for multiple columns, you just have to do it by hand.
One handy hint for quickly getting all of the column names into your query (in management studio) is to drag the Columns folder from the Object Explorer into a query window. It gives you all of the column names.