0

我在下面有 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

如果我使用“union 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
4

2 回答 2

0

这就是 UNION 在所有数据库中的工作方式。它不会创建新列,而是包含所有行。

将行转换为列是使用PIVOT运算符完成的。本质上,PIVOT 按非透视列(在本例中为区域)对行进行分组,并将聚合应用于每个生成的列。

于 2012-06-01T14:38:24.787 回答
0

您可以像其他答案建议的那样使用 pivot ,也可以将 SUM 与 CASE 结合使用来获得相同的结果。如果你有很多computedcols,这会变得很痛苦。

我在我的例子中使用了 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
于 2012-06-01T17:40:53.343 回答