2015-09-22 42 views
0

我試圖使用聯合合併兩個輸出,但這些行的值不同。我需要不同的行值爲零。如輸出(第三個) table.I與通過兩天鬥爭,請幫助我。如何合併兩個不同的行(如何分配不同的值爲零)

Select t1.round, 
    t1.SC, 
    t1.ST, 
    t1.OTHERS, 
    t2.round_up, 
    t2.SC_up, 
    t2.ST_up, 
    t2.OTHERS_up 
    From 
    (Select round as round, 
    Sum (non_slsc_qty) as SC, 
    Sum (non_slst_qty) as ST, 
    Sum (non_slot_qty) as OTHERS 
    FROM vhn_issue 
    where (date between '2015-08-01' and '2015-08-31')AND 
    dvn_cd='15' AND phc_cd='012' AND hsc_cd='05' GROUP BY round) t1 
    , 
    (Select round as round_up, 
    Sum (non_slsc_qty) as SC_up, 
    Sum (non_slst_qty) as ST_up, 
    Sum (non_slot_qty) as OTHERS_up, 
    FROM vhn_issue 
    where (date between '2015-04-01' and '2015-08-31')AND 
    dvn_cd='15' AND phc_cd='012' AND hsc_cd='05' GROUP BY round) t2 

第一個表結果

+-----------------------------------+------------+--------+-------- 
|    round    | SC  | ST  | OTHERS | 
+-----------------------------------+------------+--------+-------- 
|  1       |  20  | 30 | 50 | 
|         |   |  |  | 
|         |   |  |  | 
+-----------------------------------+------------+--------+--------+ 

這是第二個表結果

+-----------------------------------+------------+--------+---------- 
|    round_up    | SC_up | ST_up | OTHERS_up | 
+-----------------------------------+------------+--------+----------- 
|  1       |  21  | 31 | 51  | 
|  3       |  10  | 5 | 2  | 
|         |   |  |   | 
+-----------------------------------+------------+--------+--------+--- 

我需要這樣的

+------------+--------+---------------------------------------------- 
| round_up | SC  | ST |OTHERS | SC_up | ST_up |OTHERS_up | 
+------------+--------+----------------------------------------------- 
|  1  | 20 | 30 | 50 | 21 | 31 | 51  | 
|   |  |  |   |  |  |   | 
|  3  |  0 | 0 |  0 | 10 | 5 |  2 | 
+------------+--------+--------+--------------------------------------- 
+0

'選擇b.round_up,聚結(a.sc,0),聚結(a.st,0),聚結(a.others,0),b.sc_up,b.st_up, b.others_up from t1 a right join t2 b on a.round = b.round_up'試試這個 –

+0

'...,COALESCE(t2.sc,0),COALESCE(t2.st,0),... FROM (...)t1左加入()t2開...' – joop

+0

boss..it不是靜態的... –

回答

0

輸出可以使用WITH Queries (Common Table Expressions)包兩選擇並使用RIGHT JOIN來獲得所需的輸出,COALESCE用於打印0而不是NULL

WITH a 
AS (
    SELECT round AS round 
     ,Sum(non_slsc_qty) AS SC 
     ,Sum(non_slst_qty) AS ST 
     ,Sum(non_slot_qty) AS OTHERS 
    FROM vhn_issue 
    WHERE (
      DATE BETWEEN '2015-08-01' 
       AND '2015-08-31' 
      ) 
     AND dvn_cd = '15' 
     AND phc_cd = '012' 
     AND hsc_cd = '05' 
    GROUP BY round 
    ) 
    ,b 
AS (
    SELECT round AS round_up 
     ,Sum(non_slsc_qty) AS SC_up 
     ,Sum(non_slst_qty) AS ST_up 
     ,Sum(non_slot_qty) AS OTHERS_up 
     , 
    FROM vhn_issue 
    WHERE (
      DATE BETWEEN '2015-04-01' 
       AND '2015-08-31' 
      ) 
     AND dvn_cd = '15' 
     AND phc_cd = '012' 
     AND hsc_cd = '05' 
    GROUP BY round 
    ) 
SELECT coalesce(b.round_up, 0) round_up 
    ,coalesce(a.sc, 0) sc 
    ,coalesce(a.st, 0) st 
    ,coalesce(a.others, 0) others 
    ,coalesce(b.sc_up, 0) sc_up 
    ,coalesce(b.st_up, 0) st_up 
    ,coalesce(b.others_up, 0) others_up 
FROM a 
RIGHT JOIN b ON a.round = b.round_up 
+0

我有錯誤... FROM子查詢必須有別名 –

+1

感謝boss..it工作良好。 .. –

+0

我可以使用... coalesce(a.sc,0)AS SC1 –

0
WITH Results_CTE AS 
(
Select t1.round as round_up , 
    t1.SC as SC, 
    t1.ST as ST, 
    t1.OTHERS as OTHERS, 
    0 as SC_up, 
    0 as ST_up, 
    0 as OTHERS_up 
from round t1 

union all 

t2.round_up as round_up , 
    0 as SC, 
    0 as ST, 
    0 as OTHERS, 
    t2.SC_up, 
    t2.ST_up, 
    t2.OTHERS_up from round t2 
) 

select round_up , sum(SC) as SC,sum (ST) as ST, sum(OTHERS) as OTHERS, sum(SC_up) as SC_up, sum(ST_up) as ST_up, sum(OTHERS_up) as OTHERS_ up 
from Results_CTE group by round_up 
相關問題