Not sure how to rewrite the below query. I’m trying to join table_a to the most recent table_b record. Currently testing for only one ID, but a different criteria on table_a may be added:
Select t.*
from table_a t
left join table_b d on d.id = T.id and d.MOD_DATE IN (SELECT MAX(mod_date) FROM table_b d2 WHERE d2.id = t.id)
where T.id = 123456
Any suggestions?
I think you are looking for something like:
SELECT t.*
FROM table_a t
LEFT JOIN (
SELECT d.*
FROM table_b d
INNER JOIN (
SELECT id
, MAX(mod_date) mod_date_max
FROM table_b d2
GROUP BY id
) db
ON db.id = d.id
AND db.mod_date_max = d.mod_date
) d
ON d.id = T.id
WHERE T.id = 123456
Note that your where clause turns the left join into an inner join.
Also, if you get an error, please post the error message as well, not just its number.
I also found the same could be achieved with the following query:
SELECT * FROM table_a t
WHERE id IN (
SELECT id
FROM (
SELECT id,MAX(MOD_DATE)
FROM table_b
WHERE id = 123456
GROUP BY id
)
)
Related
I am working on a personal project.
I have the following data
tbl A
id, blah, blah
tbl B
id, id_2
tbl C
id_2, Result
I have check for the following, if id in tbl A exists in tbl B AND if Pass = 'A'.
I can write a query to check for the keys in A exists in B.
Something like,
SELECT * FROM tblA as a
WHERE NOT EXISTS (
SELECT * FROM tblB as b
WHERE a.id = b.id)
And this works fine.
I can also write something with an INNER JOIN
SELECT *
FROM tabC
INNER JOIN tblB ON tblC.id2 = tblB.id2
INNER JOIN tblA ON tblB.id = tblA.id
WHERE Result = 'A' GROUP BY id
But I can't seem to figure out how to combine both these queries together.
you can try like below
SELECT c.*
FROM tabC c
INNER JOIN tblB b ON c.id2 = b.id2
INNER JOIN tblA a ON b.id = a.id
WHERE Result = 'A'
and NOT EXISTS (
SELECT 1 FROM tblB as b1
WHERE a.id = b1.id)
I have a oracle database and I'm trying to query data in table1 and inner join with another table2 where one of the columns(date) is equal to the most recent date and another column in table2(built) is equal to 'yes'. This query below is not picking up the where function and can't pinpoint why
SELECT id, b, c, d
FROM table1 a
INNER JOIN table2 b on b.id = a.id
WHERE b.date =(SELECT MAX(date) FROM table2) AND b.built = 'yes'
Actual query
SELECT m_tp_str, m_tp_trn, m_tp_dte, m_tp_buy, m_tp_qtyeq, m_tp_nom, m_instr,
m_tp_p, m_tp_status2
FROM HA_PRD_DM.TP_ALL_REP a INNER JOIN HA_PRD_DM.UDF_CURR_REP b
ON a.m_udf_ref2 = b.m_nb
WHERE b.m_rep_date2 = (SELECT MAX(c.m_rep_date2) FROM HA_PRD_DM.UDF_CURR_REP c)
AND b.m_purpose = 'yes'
You can do this using analytic functions:
SELECT id, b, c, d
FROM table1 a INNER JOIN
(SELECT b.*, MAX(date) OVER (PARTITION BY b.id) as max_date
FROM table2 b
WHERE built = 'yes'
) b
ON b.id = a.id AND b.max_date = b.date;
If I execute the following code I am able to get desired output; But in Oracle is am facing error :
SELECT NAME
FROM STUD
WHERE ID IN (
SEL DISTINCT TAB1.ID
FROM (
SEL A.ID
, B.SALARY
FROM Fr A
INNER JOIN PACK B ON A.ID = B.ID
) AS TAB1
INNER JOIN (
SEL A.FRIEND_ID
, B.SALARY
FROM Fr A
INNER JOIN PACK B ON A.FRIEND_ID = B.ID
) AS TAB2
ON TAB2.SALARY > TAB1.SALARY
)
;
Facing the below error in oracle :
ORA-00907: missing right parenthesis
You have two errors: SEL instead of SELECT and the usage of table aliases with AS keyword, which is not supported by Oracle
SELECT NAME
FROM STUD
WHERE ID IN (SELECT DISTINCT TAB1.ID FROM
(SELECT A.ID , B.SALARY
FROM
Fr A
INNER JOIN
PACK B
ON
A.ID = B.ID) TAB1
INNER JOIN
(SELECT A.FRIEND_ID , B.SALARY
FROM
Fr A
INNER JOIN
PACK B
ON
A.FRIEND_ID = B.ID
) TAB2
ON
TAB2.SALARY > TAB1.SALARY
)
Besides this, I think you don't need the DISTINCT in the inner query, given that you only use it in an IN clause
I have table A, B and C
I want to return all entries in table A that do not exist in table B and of that list do not exist in table C.
select * from table_A as a
where not exists (select 1 from table_B as b
where a.id = b.id)
this gives me the first result of entries in A that are not in B. But now I want only those entries of this result that are also not in C.
I tried flavours of:
select * from table_A as a
where not exists (select 1 from table_B as b
where a.id = b.id)
AND
where not exists (select 1 from table_C as c
where a.id = c.id)
But that isnt the correct logic. If there is a way to store the results from the first query and then select * from that result that are not existent in table C. But I'm not sure how to do that. I appreciate the help.
Try this:
select * from (
select a.*, b.id as b_id, c.id as c_id
from table_A as a
left outer join table_B as b on a.id = b.id
left outer join table_C as c on c.id = a.id
) T
where b_id is null
and c_id is null
Another implementation is this:
select a1.*
from table_A as a1
inner join (
select a.id from table_A
except
select b.id from table_B
except
select c.id from table_c
) as a2 on a1.id = a2.id
Note the restrictions on the form of the sub-query as described here. The second implementation, by most succinctly and clearly describing the desired operation to SQL Server, is likely to be the most efficient.
You have two WHERE clauses in (the external part of) your second query. That is not valid SQL. If you remove it, it should work as expected:
select * from table_A as a
where not exists (select 1 from table_B as b
where a.id = b.id)
AND
not exists (select 1 from table_C as c -- WHERE removed
where a.id = c.id) ;
Tested in SQL-Fiddle (thnx #Alexander)
how about using LEFT JOIN
SELECT a.*
FROM TableA a
LEFT JOIN TableB b
ON a.ID = b.ID
LEFT JOIN TableC c
ON a.ID = c.ID
WHERE b.ID IS NULL AND
c.ID IS NULL
SQLFiddle Demo
One more option with NOT EXISTS operator
SELECT *
FROM dbo.test71 a
WHERE NOT EXISTS(
SELECT 1
FROM (SELECT b.ID
FROM dbo.test72 b
UNION ALL
SELECT c.ID
FROM dbo.test73 c) x
WHERE a.ID = x.ID
)
Demo on SQLFiddle
Option from #ypercube.Thank for the present;)
SELECT *
FROM dbo.test71 a
WHERE NOT EXISTS(
SELECT 1
FROM dbo.test72 b
WHERE a.ID = b.ID
UNION ALL
SELECT 1
FROM dbo.test73 c
WHERE a.ID = c.ID
);
Demo on SQLFiddle
I do not like "not exists" but if for some reason it seems to be more logical to you; then you can use a alias for your first query. Subsequently, you can re apply another "not exists" clause. Something like:
SELECT * FROM
( select * from tableA as a
where not exists (select 1 from tableB as b
where a.id = b.id) )
AS A_NOT_IN_B
WHERE NOT EXISTS (
SELECT 1 FROM tableC as c
WHERE c.id = A_NOT_IN_B.id
)
I have two tables, linked with an outer join. The relationship between the primary and secondary table is a 1 to [0..n]. The secondary table includes a timestamp column indicating when the record was added. I only want to retrieve the most recent record of the secondary table for each row in the primary. I have to use a group by on the primary table due to other tables also part of the SELECT. There's no way to use a 'having' clause though since this secondary table is not part of the group.
How can I do this without doing multiple queries?
For performance, try to touch the table least times
Option 1, OUTER APPLY
SELECT *
FROM
table1 a
OUTER APPY
(SELECT TOP 1 TimeStamp FROM table2 b
WHERE a.somekey = b.somekey ORDER BY TimeStamp DESC) x
Option 2, Aggregate
SELECT *
FROM
table1 a
LEFT JOIN
(SELECT MAX(TimeStamp) AS maxTs, somekey FROM table2
GROUP BY somekey) x ON a.somekey = x.somekey
Note: each table is mentioned once, no correlated subqueries
Something like:
SELECT a.id, b.*
FROM table1 a
INNER JOIN table2 b ON b.parentid = a.id
WHERE b.timestamp = (SELECT MAX(timestamp) FROM table2 c WHERE c.parentid = a.id)
Use LEFT JOIN instead of INNER JOIN if you want to show rows for IDs in table1 without any matches in table2.
select *
from table1 left outer join table2 a on
table1.id = a.table1_id
where
not exists (select 1 from table2 b where a.table1_id = b.table1_id and b.timestamp > a.timestamp)
The quickest way I know of is this:
SELECT
A.*,
B.SomeField
FROM
Table1 A
INNER JOIN (
SELECT
B1.A_ID,
B1.SomeField
FROM
Table2 B1
LEFT JOIN Table2 B2 ON (B1.A_ID=B2.A_ID) AND (B1.TimeStmp < B2.TimeStmp)
WHERE
B2.A_ID IS NULL
) B ON B.A_ID = A.ID