Sql 如何合并两个不同的行(如何分配不同的值为零)

Sql 如何合并两个不同的行(如何分配不同的值为零),sql,postgresql,join,group-by,union,Sql,Postgresql,Join,Group By,Union,我正在尝试使用union合并两个输出,但这些行的值不同。我需要不同的行的值为零。如OutputHird表。我正在努力通过两天请帮助我 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_qt

我正在尝试使用union合并两个输出,但这些行的值不同。我需要不同的行的值为零。如OutputHird表。我正在努力通过两天请帮助我

  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    |
+------------+--------+--------+---------------------------------------
您可以使用来包装这两个选择,并使用RIGHT JOIN来获得所需的输出,用于打印0而不是NULL


选择b.round_up,coalesca.sc,0,coalesca.st,0,coalesca.others,0,b.sc_up,b.st_up,b.others_up从t1 a右键连接t2 b到a.round=b.round_up试试这个…,COALESCE t2.sc,0,coalesct2.st,0。。。从…起t1左连接t2开…老板..它不是静态的…我可以使用。。。coalesca.sc,0作为SC1ok boss,但我可以像这样更改名称coalesca.sc,0作为SC1吗
    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
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