2016-04-08 15 views
0

2つの結果セットがあります。どのようにして1つの行にまとめるのですか?2つの結果セットを1つの行に結合します。

Result 1: 

    North | South| East | West 
    --------------------------- 
     5  0  0  5  

    SQL: 
    SELECT 
     SUM(CASE WHEN region = 'North' THEN threatlevel ELSE 0 END) AS North, 
     SUM(CASE WHEN region = 'South' THEN threatlevel ELSE 0 END) AS South, 
     SUM(CASE WHEN region = 'East' THEN threatlevel ELSE 0 END) AS East, 
     SUM(CASE WHEN region = 'West' THEN threatlevel ELSE 0 END) AS West, 
    FROM incident 
    GROUP BY typeOfIncident 
    HAVING typeOfIncident LIKE '%H1N5%'; 


    Result 2: 

    North1 | South1 | East1 | West1 
    -------------------------------- 
     0  5   5  8  

    SQL: 
    SELECT 
     SUM(CASE WHEN region = 'North' THEN threatlevel ELSE 0 END) AS North1, 
     SUM(CASE WHEN region = 'South' THEN threatlevel ELSE 0 END) AS South1, 
     SUM(CASE WHEN region = 'East' THEN threatlevel ELSE 0 END) AS East1, 
     SUM(CASE WHEN region = 'West' THEN threatlevel ELSE 0 END) AS West1, 
    FROM incident 
    GROUP BY typeOfIncident 
    HAVING typeOfIncident LIKE 'Zombie%'; 


    OUTPUT: 

    North | South | East | West | North1 | South1 | East1 | West1 
    -------------------------------------------------------------- 
     5  0  0  5  0   5  5  8 

私はユニオン、クロス結合などを使用しようとしました。結果は返されませんでした。どんな助けもありがとう。

答えて

0

これを試してみてください:

SELECT 
    SUM(CASE WHEN typeOfIncident LIKE '%H1N5%' AND 
       region = 'North' THEN threatlevel ELSE 0 END) AS North, 
    SUM(CASE WHEN typeOfIncident LIKE '%H1N5%' AND 
       region = 'South' THEN threatlevel ELSE 0 END) AS South, 
    SUM(CASE WHEN typeOfIncident LIKE '%H1N5%' AND 
       region = 'East' THEN threatlevel ELSE 0 END) AS East, 
    SUM(CASE WHEN typeOfIncident LIKE '%H1N5%' AND 
       region = 'West' THEN threatlevel ELSE 0 END) AS West, 
    SUM(CASE WHEN typeOfIncident LIKE 'Zombie%' AND 
       region = 'North' THEN threatlevel ELSE 0 END) AS North1, 
    SUM(CASE WHEN typeOfIncident LIKE 'Zombie%' AND 
       region = 'South' THEN threatlevel ELSE 0 END) AS South1, 
    SUM(CASE WHEN typeOfIncident LIKE 'Zombie%' AND 
       region = 'East' THEN threatlevel ELSE 0 END) AS East1, 
    SUM(CASE WHEN typeOfIncident LIKE 'Zombie%' AND 
       region = 'West' THEN threatlevel ELSE 0 END) AS West1 
FROM incident 
関連する問題