'Join multiples tables query SQL
I need to simulate a train network. I have 3 main tables in my database, and I need to create one main table with different metrics of each table.
These are my 3 tables with their columns:
Solution 1:[1]
Try This :
select
t1.date, t1.brand, t1.to, t1.met`enter code here`ric1, t1.metric2,
t2.metric3, t3.metric4
into
tain_with_metrics
from
table_1 t1
join
table_2 t2 on t1.date = t2.date
and t1.brand = t2.brand
and t1.to = t2.to
join
table_3 t3 on t3.date = t2.date
and t3.method = t2.method
and t3.type = t2.type;
Solution 2:[2]
Can you try this? Is this as per your requirement? @pierredupont
select
t1.date, t1.brand, t1.to, t1.metric1, t1.metric2,
t2.metric3, t3.metric4
into
tain_with_metrics
from
table_1 t1
left join
table_2 t2 on t1.date = t2.date
and t1.brand = t2.brand
and t1.to = t2.to
left join
table_3 t3 on t3.date = t1.date
and t3.from = t2.from
and t3.to = t1.to
inner join
table_2 t4 on t4.date = t1.date
and t4.to = t1.to
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | soniabhishek36 |
| Solution 2 | karthik_ghorpade |
