我在下面有3个选择语句,每个语句创建完全相同的计算列,然后我计算所有的是和计算列中的所有不是。目前它显示区域3次,旁边有3个结果。我希望它显示区域一次,包含3个新列,总计为
我有:
region1 computedCol1
region1 computedCol2
region1 computedCol3
region2 computedCol1
region2 computedCol2
region2 computedCol3
我想:
Region1, computedCol1, computedCol2, computedCol3
Region2, computedCol1, computedCol2, computedCol3
Region3, computedCol1, computedCol2, computedCol3
如果我使用“all all”,我只会得到:
region1 computedCol1
region2 computedCol1
region3 computedCol1
SELECT a.region, COUNT(*) AS [computedCol1]
(
SELECT DISTINCT table1.serial1, table1.serial2, region,
CASE WHEN table2.serial1 IS NULL THEN 'No' ELSE 'Yes' END AS [computedCol1],
CASE WHEN table3.serial2 IS NULL THEN 'No' ELSE 'Yes' END AS [computedCol2],
CASE WHEN table3.serial2 IS NULL AND table2.serial1 IS NULL THEN 'No' ELSE 'Yes' END AS [computedCol3]
FROM table1
LEFT JOIN table2
ON table2.serial1 = table1.serial1
LEFT JOIN dbo.EPG
table3.serial2 = table1.serial2
)a
WHERE region in (
'37000','38000','39000','41000','42000','43000','44000','46000','45000','51000','52000','53000','54000',
'55000','56000','57000','58000','59000','61000','62000','63000','64000','65000','66000','67000','68000',
'69000','30000','33000','36000','34000','35000','31000','32000','N/A' )
and [CCA Match Org] in ('no')
GROUP BY a.region
union
SELECT b.region, COUNT(*) AS [computedCol2], region
(
SELECT DISTINCT table1.serial1, table1.serial2,
CASE WHEN table2.serial1 IS NULL THEN 'No' ELSE 'Yes' END AS [computedCol1],
CASE WHEN table3.serial2 IS NULL THEN 'No' ELSE 'Yes' END AS [computedCol2],
CASE WHEN table3.serial2 IS NULL AND table2.serial1 IS NULL THEN 'No' ELSE 'Yes' END AS [computedCol3]
FROM table1
LEFT JOIN table2
ON table2.serial1 = table1.serial1
LEFT JOIN dbo.EPG
table3.serial2 = table1.serial2
)b
WHERE region in (
'37000','38000','39000','41000','42000','43000','44000','46000','45000','51000','52000','53000','54000'
'55000','56000','57000','58000','59000','61000','62000','63000','64000','65000','66000','67000','68000',
'69000','30000','33000','36000','34000','35000','31000','32000','N/A' )
and [CCA Match Org] in ('yes')
group by b.region
union
SELECT c.region, COUNT(*) AS [computedCol3], region
(
SELECT DISTINCT table1.serial1, table1.serial2,
CASE WHEN table2.serial1 IS NULL THEN 'No' ELSE 'Yes' END AS [computedCol1],
CASE WHEN table3.serial2 IS NULL THEN 'No' ELSE 'Yes' END AS [computedCol2],
CASE WHEN table3.serial2 IS NULL AND table2.serial1 IS NULL THEN 'No' ELSE 'Yes' END AS [computedCol3]
FROM table1
LEFT JOIN table2
ON table2.serial1 = table1.serial1
LEFT JOIN dbo.EPG
table3.serial2 = table1.serial2
)c
WHERE region in (
'37000','38000','39000','41000','42000','43000','44000','46000','45000','51000','52000','53000','54000'
'55000','56000','57000','58000','59000','61000','62000','63000','64000','65000','66000','67000','68000',
'69000','30000','33000','36000','34000','35000','31000','32000','N/A' )
and [CCA Match Org] in ('yes', 'no')
group by c.region
答案 0 :(得分:0)
这就是UNION在所有数据库中的工作方式。它不会创建新列,它会连接所有行。
使用PIVOT运算符将行转换为列。本质上,PIVOT按非旋转列(在本例中为Region)对行进行分组,并将聚合应用于每个生成的列。
答案 1 :(得分:0)
您可以像其他答案建议一样使用枢轴,或者您可以使用SUM结合CASE来获得相同的结果。如果你有很多计算字符,这就变得很痛苦。
我正在使用Oracle作为我的示例,但该概念应转换为SQLServer。
示例:
SELECT 'A' region, 1 col_num, 10 computed_value FROM DUAL
UNION
SELECT 'A' region, 2 col_num, 11 computed_value FROM DUAL
UNION
SELECT 'A' region, 3 col_num, 12 computed_value FROM DUAL
UNION
SELECT 'B' region, 1 col_num, 20 computed_value FROM DUAL
UNION
SELECT 'B' region, 2 col_num, 21 computed_value FROM DUAL
UNION
SELECT 'B' region, 3 col_num, 22 computed_value FROM DUAL
返回:
REGION COL_NUM COMPUTED_VALUE
A 1 10
A 2 11
A 3 12
B 1 20
B 2 21
B 3 22
如果您将该查询嵌套如下:
SELECT region,
SUM (CASE col_num WHEN 1 THEN computed_value ELSE NULL END) compulated_1,
SUM (CASE col_num WHEN 2 THEN computed_value ELSE NULL END) compulated_2,
SUM (CASE col_num WHEN 3 THEN computed_value ELSE NULL END) compulated_3
FROM (SELECT 'A' region, 1 col_num, 10 computed_value FROM DUAL
UNION
SELECT 'A' region, 2 col_num, 11 computed_value FROM DUAL
UNION
SELECT 'A' region, 3 col_num, 12 computed_value FROM DUAL
UNION
SELECT 'B' region, 1 col_num, 20 computed_value FROM DUAL
UNION
SELECT 'B' region, 2 col_num, 21 computed_value FROM DUAL
UNION
SELECT 'B' region, 3 col_num, 22 computed_value FROM DUAL)
GROUP BY region
给你:
REGION COMPULATED_1 COMPULATED_2 COMPULATED_3
A 10 11 12
B 20 21 22