0

それぞれがまったく同じ計算列を作成する下に 3 つの select ステートメントがあり、その計算列のすべての yes とすべての no を数えます。現時点では、リージョンを 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

「ユニオンオール」を使用すると、次のようになります。

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は、ピボットされていない列(この場合はRegion)で行をグループ化し、生成された各列に集計を適用します。

于 2012-06-01T14:38:24.787 に答える
0

他の回答が示唆するようにピボットを使用するか、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 に答える