I have a query that joins to several tables. Based on the column value of one table, I would like the key value of another table. But, when this key value is joined to another table (with the purpose of identifying different date values for that key), several dates appear. I would like to return the Key Value whose date is most recent when joined to another table. I have a query that works, but it is very redundant, as the sub-query is nearly identical to the main query. I didn't know if there was a technique or better way to achieve this.
Example query:
SELECT distinct TableA.key
FROM TableA a INNER JOIN TableB b
ON a.key = b.Key
INNER JOIN TableC c ON b.Key2 = c.Key2
INNER JOIN TableD d ON b.Key = d.Key
WHERE b.column1 = XYZ
AND c.column1 = 123
and d.date =
(SELECT max(d.date)
FROM TableA a INNER JOIN TableB b
ON a.Key = b.Key
INNER JOIN TableC c ON b.Key2 = c.Key2
INNER JOIN TableD d ON b.Key = d.Key
WHERE b.column1 = XYZ
AND c.column1 = 123
)
Try this. Use Top 1 with ties order by date desc
SELECT distinct TOP 1 with ties TableA.key
FROM TableA a INNER JOIN TableB b
ON a.key = b.Key
INNER JOIN TableC c ON b.Key2 = c.Key2
INNER JOIN TableD d ON b.Key = d.Key
WHERE b.column1 = XYZ
AND c.column1 = 123
order by d.date desc
Related
I'm trying to join a table on a column
Select * From tableA a
LEFT JOIN tableB b
ON a.key = b.foreignkey
...
If that row cannot be found I want to join it to another column in table B. Is there an efficient way to do this.
Thanks
You can use COALESCE to join on the first non-NULL column.
Select * From tableA a
LEFT JOIN tableB b
ON a.key = COALESCE(b.foreignkey, b.another_column, b.another_column2)
It will try to join with b.foreignkey first. if that is NULL then b.foreignkey and so on.
Please find the example here.
Add another LEFT JOIN:
Select *
From tableA a left join
tableB b
on a.key = b.foreignkey left join
tableC c
on a.key = c.foreignkey and b.foreignkey is null;
I need make a decision which table should be use in join statement depend on values in another table
I tried using CASE and COALESCE but can't achieve any success.
TableA has A and B and C and many other columns
TableB has ID and NAME columns
TableC has ID and NAME columns
My select statement is;
Select A.D, A.E, A.F From TableA A
If A.E = 1 then the following join should be used
left outer join TableB B ON A.B = B.ID
and B.NAME should be returned in the select statement
If A.E = 2 then the following join should be used
left outer join TableC C ON A.B = C.ID
and C.NAME should be returned in the select statement
Just add your conditions to the joins, and then use a case statement to pull the correct field to your result set e.g.
select A.D, A.E, A.F
, case when B.[Name] is not null then B.[Name] else C.[Name] end [Name]
from TableA A
left outer join TableB B ON A.B = B.ID and A.E = 1
left outer join TableC C ON A.B = C.ID and A.E = 2
Join tablea with the union of tableb with an extra column with value 1 and tablec with an extra column with value 2 and apply the conditions in the ON clause:
select
a.D, a.E, a.F, u.NAME
from tablea a
left join (
select *, 1 col from tableb
union all
select *, 2 col from tablec
) u on a.B = u.id and a.E = u.col
I've got two queries that return single result.
They look something like this
// query 1
SELECT A.id FROM tableA A
INNER JOIN tableB B
ON B.id = A.id
WHERE b.status = 'ACTIVE'
// query 2
SELECT C.id FROM tableC C
WHERE c.status = 'ACTIVE'
How to combine them and make return the pair of values instead of one value from different queries? I mean to get something like [A.id, C.id]
Currently I have to use two queries in the applications and I want to combine them into one.
I think like this will do
SELECT (SELECT A.id FROM tableA A
INNER JOIN tableB B
ON B.id = A.id
WHERE b.status = 'ACTIVE'
) as 'query1',
(
SELECT C.id FROM tableC C
WHERE c.status = 'ACTIVE'
) as 'query2'
As your question is not clear, so i assume that you either needids from mentioned queries in one row or in different rows, you can use union all/union (provided that datatypes are compatible or implicitly convertible and duplicates or allowed or not) as below.
Combining Result in different rows.
SELECT A.id
FROM tableA A
INNER JOIN tableB B
ON B.id = A.id
WHERE b.status = 'ACTIVE'
union all
SELECT C.id
FROM tableC C
WHERE c.status = 'ACTIVE'
Combining Result in Single Row.
select max(id1), max(id2)
from(
SELECT A.id as id1, NULL as id2
FROM tableA A
INNER JOIN tableB B
ON B.id = A.id
WHERE b.status = 'ACTIVE'
union all
SELECT NULL, C.id
FROM tableC C
WHERE c.status = 'ACTIVE'
) t;
SAMPLE DEMO
You can run following query which work fine for me:
select t1.id as aid ,t2.id as cid
from (
SELECT A.id
FROM tableA A
INNER JOIN tableB B ON B.id = A.id
WHERE b.status = 'ACTIVE'
) t1
full outer join (
SELECT C.id
FROM tableC C
WHERE c.status = 'ACTIVE'
) t2 on t1.id=t2.id
You can join your second query with your first query as follows, so that you will get two (A.id, C.id) values in one query...
SELECT A.ID,C.ID FROM
(SELECT A.ID FROM table_A A INNER JOIN
table_B B ON A.ID=B.ID WHERE B.STATUS='A')A
INNER JOIN table_c C
ON C.ID=A.ID WHERE C.STATUS='A';
Say you have 3 tables (tableA, tableB, tableC), each with an ID column and a Value column. Some of the tables' IDs match but some don't.
If you do:
SELECT tableA.ID FROM tableA
FULL JOIN tableB ON (tableA.ID = tableB.ID)
FULL JOIN tableC ON (tableA.ID = tableC.ID)
Is this different from:
SELECT tableA.ID FROM tableA
FULL JOIN tableB ON (tableA.ID = tableB.ID)
FULL JOIN tableC ON (tableB.ID = tableC.ID)
Or:
SELECT Y.ID FROM
(SELECT tableA.ID FROM tableA
FULL JOIN tableB ON (tableA.ID = tableB.ID)) X
FULL JOIN tableC ON (X.ID = tableC.ID)) Y
??? Someone please explain if there is a difference. Thanks.
[Oracle SQL Developer version 4.02.15.21]
For starters, here are all 3 statements, syntactically cleaned up:
SELECT COALESCE(a.ID,b.ID,c.ID)
FROM tableA a
FULL JOIN tableB b ON a.ID = b.ID
FULL JOIN tableC c ON a.ID = c.ID
SELECT COALESCE(a.ID,b.ID,c.ID)
FROM tableA a
FULL JOIN tableB b ON a.ID = b.ID
FULL JOIN tableC c ON b.ID = c.ID
SELECT COALESCE(X.ID,c.ID)
FROM
( SELECT COALESCE(a.ID ,b.ID) ID
FROM tableA a
FULL JOIN tableB b ON a.ID = b.ID) X
FULL JOIN tableC c ON X.ID = c.ID
Surprisingly, the syntax of the first statement produces duplicate values, but statements 2 and 3 work as advertised.
Edit: Upon further testing, statements 1 and 2 are prone to duplicates, depending on which tables overlap. Statement 3 seems to be the only solid approach.
SQLFiddle
i have four tables which are related like this:
TABLE A: 1 to many with TABLE B
TABLE B: 1 to many with TABLE C
TABLE D: many to 1 with TABLE B
I would like to create a result set which contains no duplicates.
SELECT A.f1
A.f2
B.f1
C.f1
D.f1
from A LEFT JOIN (B INNER JOIN D on D.fk_b = B.id) on A.id = B.fk_a
LEFT JOIN C on C.fk_b = B.id
Some but not all records are duplicated. Only the records from TABLE D which have the same FK to TABLE B.
If there are 2 the same D.fk_B fields then i would like to select only the first one. My INNER JOIN would become something like:
B INNER JOIN TOP 1 D on.....
But that's not possible?
Thank you!
Assuming that the structure in question is near the one shown bellow,
The following SELECT statement will show you just the primary keys, briefly giving an idea about what data is being returned.
SELECT
A.id AS `A_id`,
B.id AS `B_id`,
COALESCE(C.id, 0) AS `C_id`,
COALESCE(
(SELECT D.id FROM D WHERE D.fk_b = B.id LIMIT 1), 0
) AS `D_id`
FROM B
INNER JOIN A ON (B.fk_a = A.id)
LEFT JOIN C ON (B.id = C.fk_b);
Now the select you've asked for:
SELECT
A.f1 AS `A_f1`,
A.f2 AS `A_f2`,
B.f1 AS `B_f1`,
COALESCE(C.f1, '-') AS `C_f1`,
COALESCE(
(SELECT D.f1 FROM D WHERE D.fk_b = B.id LIMIT 1), '-'
) AS `D_f1`
FROM B
INNER JOIN A ON (B.fk_a = A.id)
LEFT JOIN C ON (B.id = C.fk_b);
Code above was tested and worked fine on MySQL 5.6
It appears that you have cartesian join.
You could try just putting DISTINCT infront of you column definition or you could so subqueries to link the tables together.
Try
SELECT DISTINCT A.f1
A.f2
B.f1
C.f1
D.f1
from A LEFT JOIN (B INNER JOIN D on D.fk_b = B.id) on A.id = B.fk_a
LEFT JOIN C on C.fk_b = B.id
You can do this:
B INNER JOIN TOP 1 D on.....
Like this
B INNER JOIN (SELECT TOP 1 fields from table) D ON ...
Try this:
SELECT DISTINCT A.f1
A.f2
B.f1
C.f1
D.f1
from A
LEFT JOIN (B INNER JOIN (SELECT DISTINCT fk_b FROM D) D on D.fk_b = B.id)
on A.id = B.fk_a
LEFT JOIN C on C.fk_b = B.id