Inner join on ConCat fields - sql

I have table A
Code Range
A 12569
B 18175
C 478931
And Table B
id Type
A12569 0
B18175 1
C478931 0
How can I concatenate the two fields of the first table, in order to join them with the second table.
I have tried with the following query
SELECT concat(A.code,B.Range),b.Type FROM DB.tableA A
inner join DB.tableB B
on Concat(A.code,B.Range)= B.id;

Just concatenate the two columns:
select *
from table_a a
join table_b b on a.code||range = b.id;
The above is standard SQL - not all DBMS respect that though and use a different operator to concatenate strings.

SQL Server:
Select a.Code+b.Range as id, b.Type from TableA a inner join tableB b on a.Code + b.Range = b.id
Untested but should work - presumption is all columns are varchar or nvarchar. May need to add some casting on the range field if that's not the case.

Related

Join two tables on a field contais other field

How to join two tables if a field contains other field? Example:
On table A I have a field with data '000;111;222' and on table B I have a field with data '111'.
I want to join like this:
select * from A join B on A.field contains B.field
You could do:
select a.*, b.*
from a
inner join b on b.field = any(string_to_array(a.field, ';'))
The join condition turns a.field to an array, then checks if it contains b.field.
Well perhaps you are giving string_to_array the incorrect parameters. As alternative you can use the POSITION function to find if there is a sbustring match.
with table_a (acol) as ( values('000;111;222'),('000;xxx;222') )
, table_b (bcol) as ( values ('111'),('xxx'),('000'),('123') )
select *
from table_a
join table_b on POSITION(bcol in acol) > 0;

SQL Get rows that doesn't appear in another table

I have this SQL problem: I have tables A and B. Table A has columns id and name, Table B amount and id which is a foreign key to table A.id.
I need to return all table A rows that don't have their id stored in table B. Any ideas?
So the complete opposite is:
SELECT *
FROM a
LEFT OUTER JOIN b ON a.id = b.id;
Here row what I need is left out of result
Just add a where clause:
SELECT a.*
FROM a LEFT OUTER JOIN
b
ON a.id = b.id
WHERE b.id IS NULL;
You can also use NOT EXISTS:
select a.*
from a
where not exists (select 1 from b where b.id = a.id);
In most databases, the two methods typically have similar performance.

Query left join without all the right rows from B table

I have 2 tables, A and B.
I need all columns from A + 1 column from B in my select.
Unfortunately, B has multiples rows(all identicals) for 1 row in A
on the join condition.
I tried but I can't isolate one row in A for one row in B with left join for example while keeping my select.
How can I do this query ? Query in ORACLE SQL
Thanks in advance.
This is a good use for outer apply. The structure of the query looks like this:
select a.*, b.col
from a outer apply
(select top 1 b.col
from b
where b.? = a.?
) b;
Normally, you would only use top 1 with order by. In this case, it doesn't seem to make a difference which row you choose.
You can group by on all columns from A, and then use an aggregate (like max or min) to pick any of the identical B values:
select a.*
, b.min_col1
from TableA a
left join
(
select a_id
, min(col1) as min_col1
from TableB
group by
a_id
) b
on b.a_id = a.id

Exclude using joins without subquery

how to converse following code to get the same results using join (without using subquery)
select a_key from table_a a
inner join table_b b --in my code I've 5 joins like that
on a.a_key=b.a_key
where a_key not in
(select a_key from table_c --and conditions within this brackets also
where var_a beteween table_c.col1 and table_c.col2
or var_b beteween table_c.col1 and table_c.col2
)
The following is essentially the same logic:
select a_key
from table_a a inner join
table_b b
on a.a_key = b.a_key left join
table_c c
on (var_a between table_c.col1 and table_c.col2 or
var_b between table_c.col1 and table_c.col2
) and
a.a_key = c.a_key
where c.a_key is null;
You should prefix your columns with table aliases. The column a_key is ambiguous in your original, as are the column var_a and var_b.
These are slightly different if any matching table_c.a_key values are NULL. In that case, the join version probably behaves more like you would expect.

Can this be done with a single SQL Join?

I am not sure if this can be done with a single JOIN, but I basically have two tables with an ID column in common. To make it simple I'll say Table A just contains an ID while Table B contains an ID and Code. There is a 1:M relationship between Table A and Table B, however it's also possible an ID from Table A is not contained in Table B at all. I was hoping to have a query return every ID that exists in Table B within a particular code range, or does not exist in Table B at all.
I tried using a LEFT JOIN with something like:
SELECT A.id FROM A LEFT JOIN B ON A.id = B.id AND b.code BETWEEN '000' AND '123'
But, this still gives me the IDs that exist in Table B outside of the code range.
Use a left join, and filter the result to contain the codes in the range, and also the lines where there is no matching record in table B:
select
A.id
from
A
left join B on B.id = A.id
where
B.code between '000' and '123' or B.id is null
What about
SELECT id FROM A LEFT JOIN B ON A.id = B.id
WHERE b.code IS NULL OR b.code BETWEEN ' ' AND '123'