我想将多个查询的结果合并到一个结果中。所有查询都返回两个字段,其中一个是常用字段,即名称字段。我想使用名称字段合并结果。
示例:
首先查询结果:
Value Name
----- ------
30 John
40 Nick
50 Liam
第二个查询结果:
Value2 Name
----- ------
30 John
40 Nick
50 Joe
第三个查询结果:
Value3 Name
----- ------
30 John
40 Tony
50 Jack
我希望结果看起来像这样:
Value Value2 Value3 Name
..... ...... ..... ....
30 30 30 John
40 40 Nick
50 Liam
50 Joe
40 Tony
50 Jack
答案 0 :(得分:2)
您可以使用union all
和聚合:
select name,
max(value1) as value1,
max(value2) as value2,
max(value3) as value3
from ((select name, value as value1, null as value2, null as value3
from q1
) union all
(select name, null as value1, value as value2, null as value3
from q2
) union all
(select name, null as value1, null as value2, value as value3
from q3
)
) q
group by name;