简体   繁体   English

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

[英]Add sum of first query to sum of second query in SQL

I have an existing query that sums a column: 我有一个汇总一列的现有查询:

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;     

the example output will be: 示例输出将是:

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

and I have another query of which I would like to sum up the forecast total as following: 我还有另一个查询,希望将预测总数汇总如下:

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;

` `

The output for the above query would be: 以上查询的输出为:

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

I would like to add the the two outputs for whenever the first 3 columns match and include a new total of forecast sales like this: 我想在前三列匹配时添加两个输出,并包括这样的新预测销售总额:

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

If the rows do not match in the 2nd/3rd column I would still like to see their sum. 如果第2 / 3rd列中的行不匹配,我仍然希望看到它们的总和。 Any idea how I can possibly do that with SQL? 知道我怎么能用SQL做到这一点吗?

Thanks! 谢谢!

I would try to UNION your two queries and then aggregate them on top. 我会尝试合并两个查询,然后将它们汇总在最前面。 So, something like this 所以像这样

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

Combined with your statements above, this yields the following long-ish SQL statement: 结合上面的语句,将产生以下冗长的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

To reduce the complexity of this SQL statement (and thus to improve maintainability), it might be helpful to make use of SQL views in this case. 为了降低此SQL语句的复杂性(从而提高可维护性),在这种情况下使用SQL视图可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM