说我有3张这样的表:
Table 1
|Id|Date |Data1|
|--|---- |-----|
|1 |24/05|Some1|
Table 2
|Id|Date |Data2|
|--|---- |-----|
|1 |24/05|Some2|
|1 |12/06|Some2|
Table 3
|Id|Date |Data3|
|--|---- |-----|
|1 |24/05|Some3|
|1 |14/06|Some3|
我如何得到如此结果:
|Id|Date |Data1|Data2|Data3|
|--|-----|-----|-----|-----|
|1 |24/05|Some1|Some2|Some3|
|1 |12/06|NULL |Some2|NULL |
|1 |14/06|NULL |NULL |Some3|
使用MS SQL。
我想出的是
SELECT Id, Date, Data1, Data2, Data3 FROM Table1
LEFT JOIN Table2 ON Table1.Id = Table2.Id AND Table1.Date = Table2.Date
LEFT JOIN Table3 ON Table1.Id = Table3.Id AND Table1.Date = Table3.Date
UNION
SELECT Id, Date, Data1, Data2, Data3 FROM Table2
LEFT JOIN Table1 ON Table2.Id = Table1.Id AND Table2.Date = Table1.Date
LEFT JOIN Table3 ON Table2.Id = Table3.Id AND Table2.Date = Table3.Date
UNION
SELECT Id, Date, Data1, Data2, Data3 FROM Table3
LEFT JOIN Table1 ON Table3.Id = Table1.Id AND Table3.Date = Table3.Date
LEFT JOIN Table2 ON Table3.Id = Table2.Id AND Table3.Date = Table2.Date
有没有更好的方法来查询数据?
答案 0 :(得分:0)
快速方法是对临时表中的所有日期和ID进行排序,然后对其进行数据处理。
Select T.id,T.date, T1.Data1,T2.Data2,T3.Data3
FROM
(
select id,date from Table1
UNION
select id,date from Table2
UNION
select id,date from Table3
) T
LEFT JOIN Table1 T1 on T.id=T1.id and T.date=T1.date
LEFT JOIN Table2 T2 on T.id=T2.id and T.date=T2.date
LEFT JOIN Table3 T3 on T.id=T3.id and T.date=T3.date
ORDER BY T.id,T.date
JOIN数量=唯一表数
答案 1 :(得分:0)
select
id = coalesce(t1.id,t2.id,t3.id)
, [date] = coalesce(t1.[date],t2.[date],t3.[date])
, t1.Data1
, t2.Data2
, t3.Data3
from Table1 t1
full join Table2 t2
on t1.id = t2.id
and t1.[date]=t2.[date]
full join Table3 t3
on (t1.id = t3.id and t1.[date]=t3.[date])
or (t2.id = t3.id and t2.[date]=t3.[date])
rextester演示:http://rextester.com/ATCJ12705
返回:
+----+-------+-------+-------+-------+
| id | date | Data1 | Data2 | Data3 |
+----+-------+-------+-------+-------+
| 1 | 24/05 | Some1 | Some2 | Some3 |
| 1 | 12/06 | NULL | Some2 | NULL |
| 1 | 14/06 | NULL | NULL | Some3 |
+----+-------+-------+-------+-------+
答案 2 :(得分:0)
假设每个表的每个id和日期只有1行,将空值放在UNION中并选择MAX值可以达到相同的效果:
create table #t1 (id int, [date] nvarchar(5), data1 nvarchar(10))
create table #t2 (id int, [date] nvarchar(5), data2 nvarchar(10))
create table #t3 (id int, [date] nvarchar(5), data3 nvarchar(10))
insert into #t1 values (1, '24/05', 'Some1')
insert into #t2 values (1, '24/05', 'Some2'), (1, '12/06', 'Some2')
insert into #t3 values (1, '24/05', 'Some3'), (1, '14/06', 'Some3')
select a.id, a.[date], max(a.data1) as [Data1], max(a.data2) as [Data2], max(a.data3) as [Data3]
from
(
select id, [date], [data1] as [Data1], Null as [Data2], null as [Data3] from #t1
union
select id, [date], null as [Data1], [data2] as [Data2], null as [Data3] from #t2
union
select id, [date], null as [Data1], Null as [Data2], [Data3] as [Data3] from #t3
) a
group by a.id, a.[date]