在SQL中将第一个查询的总和添加到第二个查询的总和

时间:2016-05-20 19:52:06

标签: sql

我有一个对列进行求和的现有查询:

SELECT  
     fcst.eow_date - 21 AS eow_date,
     fcst.item,
     ril.source_wh,
     SUM (fcst.forecast_sales) AS forecast_sales
FROM RMS10.item_forecast fcst, RMS10.repl_item_loc ril, RMS10.wh w
WHERE  ril.item = fcst.item
     AND fcst.loc = ril.location
     AND ril.source_wh = w.wh
     AND w.forecast_wh_ind = 'Y'
     AND ril.loc_type = 'S'
     AND fcst.item IN (SELECT UNIQUE item
                         FROM RMS10.repl_item_loc ril, RMS10.wh w
                        WHERE     ril.location = W.WH
                              AND W.FORECAST_WH_IND = 'Y'
                              AND RIL.REPL_METHOD IN ('TI',
                                                      'M',
                                                      'C',
                                                      'D'))
GROUP BY fcst.item, eow_date, source_wh
ORDER BY 3, 2, 1;     

示例输出将是:

4/30/2016   9953639 159384  184.5015
5/7/2016    9953639 159384  188.5844
5/14/2016   9953639 159384  186.102`

我还有另一个问题,我想总结预测总数如下:

SELECT fcst.eow_date -21 AS eow_data, fcst.item, axrf.source_whse, SUM(fcst.forecast_sales) AS forecast_sales
FROM item_forecast fcst, aip.aafes_pack_item_xref axrf, repl_item_loc rpl
WHERE fcst.item = axrf.item
     AND fcst.item = rpl.item
     AND fcst.loc = rpl.location
     AND rpl.source_wh = axrf.loc
     AND rpl.loc_type = 'S'
GROUP BY fcst.eow_date, fcst.item, axrf.source_whse
ORDER BY 3,2,1;

`

上述查询的输出为:

4/30/2016   9953639 159384  58.1433
5/7/2016    9953639 159384  56.5777
5/14/2016   9953639 159384  57.5736

我想在前3列匹配时添加两个输出,并包含新的预测销售总额,如下所示:

4/30/2016   9953639 159384  242.6448
5/7/2016    9953639 159384  245.1621
5/14/2016   9953639 159384  243.6756

如果第二/第三列中的行不匹配,我仍然希望看到它们的总和。知道如何用SQL做到这一点吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我会尝试联合您的两个查询,然后将它们聚合在一起。所以,像这样

select eow_date, item, sum(forecast_sales) as forecast_sales
from
(
   ( your-first-query )
   UNION ALL
   ( your-second-query )
) as union_table
group by eow_date, item

结合上面的陈述,这会产生以下长期的SQL语句:

SELECT eow_date, item, sum(forecast_sales) AS forecast_sales
FROM 
(
   ( 
     SELECT  
     fcst.eow_date - 21 AS eow_date,
     fcst.item,
     ril.source_wh,
     SUM (fcst.forecast_sales) AS forecast_sales
     FROM RMS10.item_forecast fcst, RMS10.repl_item_loc ril, RMS10.wh w
     WHERE  ril.item = fcst.item
        AND fcst.loc = ril.location
        AND ril.source_wh = w.wh
        AND w.forecast_wh_ind = 'Y'
        AND ril.loc_type = 'S'
        AND fcst.item IN (SELECT UNIQUE item
                     FROM RMS10.repl_item_loc ril, RMS10.wh w
                    WHERE     ril.location = W.WH
                          AND W.FORECAST_WH_IND = 'Y'
                          AND RIL.REPL_METHOD IN ('TI',
                                                  'M',
                                                  'C',
                                                  'D'))
     GROUP BY fcst.item, eow_date, source_wh
     ORDER BY 3, 2, 1
   )
   UNION ALL
   (
      SELECT fcst.eow_date -21 AS eow_data, fcst.item, axrf.source_whse, SUM(fcst.forecast_sales) AS forecast_sales
      FROM item_forecast fcst, aip.aafes_pack_item_xref axrf, repl_item_loc rpl
      WHERE fcst.item = axrf.item
         AND fcst.item = rpl.item
         AND fcst.loc = rpl.location
         AND rpl.source_wh = axrf.loc
         AND rpl.loc_type = 'S'
      GROUP BY fcst.eow_date, fcst.item, axrf.source_whse
      ORDER BY 3,2,1
   )
) AS union_table
GROUP BY eow_date, item

为了降低此SQL语句的复杂性(从而提高可维护性),在这种情况下使用SQL视图可能会有所帮助。