从 UNION ALL 结果集中获取总和
Getting the sum from a UNION ALL result set
我有一个 SQL 查询,它在 UNION ALL 查询中从 window 时间段的每一天获取几个数据点,然后在结果集中对每一天的数据求和。
我要添加到结果集中的是结果集中所有日期底部的总计。我尝试使用 ROLLUP(使用 GROUP BY 语句)但没有成功,而且我一直收到错误 ORA-00933: SQL command not properly ended
.
不确定是否有使用 UNION ALL 进行 ROLLUP 的技巧,或者它是否根本不起作用。我还尝试了一个临时 table,然后用临时 table 的总和执行 UNION ALL,但这看起来太复杂了(而且我也无法让它工作)。
有人可以帮助指导我吗?这是我的工作代码:
SELECT /*+ USE_HASH(x) */
ACTIVITY_DAY
, SUM(ORDERED_UNITS)
, SUM(ORDERED_AMT)
FROM (
SELECT ACTIVITY_DAY,ORDERED_UNITS,ORDERED_AMT FROM D_DAILY_ORDERS
WHERE REGION_ID = 1
AND MARKETPLACE_ID = 1
AND ACTIVITY_DAY BETWEEN TO_DATE('20160409','YYYYMMDD') AND TO_DATE('20160505','YYYYMMDD')
AND ID IN ('B019433MGC', 'B019433O4C', 'B00STUX0F0', 'B017SL40S6', 'B00ZHJDJ5W', 'B00STUX276', 'B017SL44RI', 'B00ST415S0', 'B00ST414O0')
UNION ALL
SELECT ACTIVITY_DAY,ORDERED_UNITS,ORDERED_AMT FROM D_DAILY_ORDERS
WHERE REGION_ID = 1
AND MARKETPLACE_ID = 1
AND ACTIVITY_DAY BETWEEN TO_DATE('20160501','YYYYMMDD') AND TO_DATE('20160505','YYYYMMDD')
AND ID IN ('B007V4YR5M', 'B00BHQ9CHI')
) x
GROUP BY ACTIVITY_DAY
ORDER BY ACTIVITY_DAY
;
如果我理解得很好,ROLLUP 是正确的方法;您可能遇到了一些语法错误:
SQL> SELECT
2 ACTIVITY_DAY
3 , SUM(ORDERED_UNITS)
4 , SUM(ORDERED_AMT)
5 FROM (
6 select sysdate as ACTIVITY_DAY, 1 as ORDERED_UNITS, 10 as ORDERED_AMT from dual union all
7 select sysdate-1 as ACTIVITY_DAY, 2 as ORDERED_UNITS, 20 as ORDERED_AMT from dual union all
8 select sysdate-1 as ACTIVITY_DAY, 2 as ORDERED_UNITS, 20 as ORDERED_AMT from dual
9 ) x
10 GROUP BY rollup(ACTIVITY_DAY)
11 ORDER BY ACTIVITY_DAY
12 ;
ACTIVITY_ SUM(ORDERED_UNITS) SUM(ORDERED_AMT)
--------- ------------------ ----------------
15-MAG-16 4 40
16-MAG-16 1 10
5 50
我有一个 SQL 查询,它在 UNION ALL 查询中从 window 时间段的每一天获取几个数据点,然后在结果集中对每一天的数据求和。
我要添加到结果集中的是结果集中所有日期底部的总计。我尝试使用 ROLLUP(使用 GROUP BY 语句)但没有成功,而且我一直收到错误 ORA-00933: SQL command not properly ended
.
不确定是否有使用 UNION ALL 进行 ROLLUP 的技巧,或者它是否根本不起作用。我还尝试了一个临时 table,然后用临时 table 的总和执行 UNION ALL,但这看起来太复杂了(而且我也无法让它工作)。
有人可以帮助指导我吗?这是我的工作代码:
SELECT /*+ USE_HASH(x) */
ACTIVITY_DAY
, SUM(ORDERED_UNITS)
, SUM(ORDERED_AMT)
FROM (
SELECT ACTIVITY_DAY,ORDERED_UNITS,ORDERED_AMT FROM D_DAILY_ORDERS
WHERE REGION_ID = 1
AND MARKETPLACE_ID = 1
AND ACTIVITY_DAY BETWEEN TO_DATE('20160409','YYYYMMDD') AND TO_DATE('20160505','YYYYMMDD')
AND ID IN ('B019433MGC', 'B019433O4C', 'B00STUX0F0', 'B017SL40S6', 'B00ZHJDJ5W', 'B00STUX276', 'B017SL44RI', 'B00ST415S0', 'B00ST414O0')
UNION ALL
SELECT ACTIVITY_DAY,ORDERED_UNITS,ORDERED_AMT FROM D_DAILY_ORDERS
WHERE REGION_ID = 1
AND MARKETPLACE_ID = 1
AND ACTIVITY_DAY BETWEEN TO_DATE('20160501','YYYYMMDD') AND TO_DATE('20160505','YYYYMMDD')
AND ID IN ('B007V4YR5M', 'B00BHQ9CHI')
) x
GROUP BY ACTIVITY_DAY
ORDER BY ACTIVITY_DAY
;
如果我理解得很好,ROLLUP 是正确的方法;您可能遇到了一些语法错误:
SQL> SELECT
2 ACTIVITY_DAY
3 , SUM(ORDERED_UNITS)
4 , SUM(ORDERED_AMT)
5 FROM (
6 select sysdate as ACTIVITY_DAY, 1 as ORDERED_UNITS, 10 as ORDERED_AMT from dual union all
7 select sysdate-1 as ACTIVITY_DAY, 2 as ORDERED_UNITS, 20 as ORDERED_AMT from dual union all
8 select sysdate-1 as ACTIVITY_DAY, 2 as ORDERED_UNITS, 20 as ORDERED_AMT from dual
9 ) x
10 GROUP BY rollup(ACTIVITY_DAY)
11 ORDER BY ACTIVITY_DAY
12 ;
ACTIVITY_ SUM(ORDERED_UNITS) SUM(ORDERED_AMT)
--------- ------------------ ----------------
15-MAG-16 4 40
16-MAG-16 1 10
5 50