我希望将两个查询与多个列结合起来,就像在其他帖子中推荐的那样
select NULL AS Col1
,NULL AS Col2
,NULL AS Col3
,Col4
,Col5
union
select Col1
,Col2
,Col3
,NULL AS Col4
,NULL AS Col5
我明白了:
Col1 | Col2 | Col3 | Col4 | Col5
---------------------------------
NULL | NULL | NULL | 2 | 0
7 | 3 | 4 | NULL | NULL
有没有办法合并行? 由于这个别名我不能按列分组。
我想得到
Col1 | Col2 | Col3 | Col4 | Col5
---------------------------------
7 | 3 | 4 | 2 | 0
答案 0 :(得分:1)
根据DhruvJoshi的回答,你可以尝试这样,
SELECT B.Col1
,B.col2
,B.col3
,A.col4
,A.col5
FROM (
SELECT NULL AS Col1
,NULL AS Col2
,NULL AS Col3
,2 AS Col4
,0 AS Col5
) A
CROSS JOIN (
SELECT 7 AS Col1
,3 AS Col2
,4 AS Col3
,NULL AS Col4
,NULL AS Col5
) B
答案 1 :(得分:0)
是的,您可以将CROSS JOIN用于下面的用例
select Col1 ,Col2 ,Col3, Col4 ,Col5 from tblA CROSS JOIN tblB
答案 2 :(得分:0)
试试这个:
SELECT SUM(Col1) AS 'Col1', SUM(Col2) AS 'Col2', SUM(Col3) AS 'Col3', SUM(Col4) AS 'Col4', SUM(Col5) AS 'Col5' FROM
(
select NULL AS Col1
,NULL AS Col2
,NULL AS Col3
,Col4
,Col5
union
select Col1
,Col2
,Col3
,NULL AS Col4
,NULL AS Col5
) A