Suppose I have the following three tables:
Table1:
ID | Value_1 |
---|---|
11 | abc |
22 | def |
33 | xyz |
Table2:
ID | Date_1 |
---|---|
11 | 12-Mar-22 |
11 | 01-Jan-23 |
22 | 19-Dec-22 |
22 | 07-Feb-23 |
33 | 07-Mar-22 |
Table3:
ID | Length_1 |
---|---|
11 | 574 |
11 | 1029 |
22 | 9220 |
33 | 1093 |
33 | 876 |
Now, I need an SQL query that would select each ID with Max Lenth_1 and Max Date_1.
Desired output:
ID | Value_1 | Date_1 | Length_1 |
---|---|---|---|
11 | abc | 01-Jan-23 | 1029 |
22 | def | 07-Feb-23 | 9220 |
33 | xyz | 07-Mar-22 | 1093 |
I have used max() fuction to achieve this with left join between 2 tables together, however struggling when I have to use Max () twice with 3 tables. I am relatively new to SQL.
SQL Select Max(Date) out of rows with Duplicate Id I tried this for two tables
Select t1.id, t1.value1, max(t2.date_1) date_1, max(t3.length_1)t3
from table_1 t1
left join table_2 t2 on t1.id=t2.id
left join table_3 t3 on t1.id=t3.id
group by t1.id, t1.value1
order by 1