I have a query which returns data grouped by Month End for dates last month but by date for dates MTD. I am able to use group by ROLLUP() to successfully generate a grand total, but I would like to calculate a MTD total as well.
Using the following query:
select
COALESCE(to_char(case when postdate < trunc(sysdate,'MM')
then last_day(postdate)
else postdate end,'mm/dd/yyyy'),'Grand Total') MonthEnd,
count(colA) NumColA,
count(colB) NumColB,
count(colC) NumColC
from Table1
where postdate >= trunc(sysdate,'YY') and postdate < trunc(sysdate,'DD')
GROUP BY ROLLUP
(
COALESCE(to_char(case when postdate < trunc(sysdate,'MM')
then last_day(postdate)
else postdate end,'mm/dd/yyyy'),'Grand Total')
)
Which gives me an (expected) result:
Month End NumColA NumColB NumColC
1/31/2020 10 25 100
2/29/2020 10 35 150
3/31/2020 10 40 300
4/1/2020 1 3 61
…
4/7/2020 4 8 11
Grand Total 35 111 622
What I want to see is a sub total for April MTD only before the grand Total, like so:
Month End NumColA NumColB NumColC
1/31/2020 10 25 100
2/29/2020 10 35 150
3/31/2020 10 40 300
4/1/2020 1 3 61
…
4/7/2020 4 8 11
MTD 5 11 72
Grand Total 35 111 622
Apologies for the crappy table spacing.
GROUPING SETS and GROUPING_ID functions can help here.
with t (MonthEnd, NumColA, NumColB, NumColC) as (
select to_date('1/31/2020', 'mm/dd/yyyy'), 10, 25, 100 from dual union all
select to_date('2/29/2020', 'mm/dd/yyyy'), 10, 35, 150 from dual union all
select to_date('3/31/2020', 'mm/dd/yyyy'), 10, 40, 300 from dual union all
select to_date('4/1/2020', 'mm/dd/yyyy'), 1, 3, 61 from dual union all
select to_date('4/2/2020', 'mm/dd/yyyy'), 5, 9, 08 from dual union all
select to_date('4/7/2020', 'mm/dd/yyyy'), 4, 8, 11 from dual
)
, prep as (
select MonthEnd
, case when trunc(sysdate, 'month') = trunc(monthend, 'month') then trunc(sysdate, 'month') end grp_dt
, NumColA, NumColB, NumColC
from t
)
select grouping_id (monthend, grp_dt) grp, monthend, grp_dt
, case when grouping_id (monthend, grp_dt) = 2 and grp_dt is not null then 'MTD'
when grouping_id (monthend, grp_dt) = 2 and grp_dt is null then 'Prev'
when grouping_id (monthend, grp_dt) = 3 then 'Grand Total'
else to_char(monthend, 'mm/dd/yyyy') end l
, sum(NumColA) sa, sum(numcolb) sb, sum(numcolc) sc
from prep
group by grouping sets((monthend, grp_dt), (grp_dt), ());
GRP MONTHEND GRP_DT L SA SB SC
---------- --------- -------------------- ----------- ---------- ---------- ----------
0 31-JAN-20 01/31/2020 10 25 100
0 29-FEB-20 02/29/2020 10 35 150
0 31-MAR-20 03/31/2020 10 40 300
2 Prev 30 100 550
0 01-APR-20 01-APR-20 04/01/2020 1 3 61
0 02-APR-20 01-APR-20 04/02/2020 5 9 8
0 07-APR-20 01-APR-20 04/07/2020 4 8 11
2 01-APR-20 MTD 10 20 80
3 Grand Total 40 120 630
9 rows selected.
I have kept all the columns for debugging purposes and the first 3 columns can be removed from the outermost SELECT. If you are not interested in showing the Prev
row, just wrap it with another outer query and apply WHERE TXT != 'Prev'
.
with t (MonthEnd, NumColA, NumColB, NumColC) as (
select to_date('1/31/2020', 'mm/dd/yyyy'), 10, 25, 100 from dual union all
select to_date('2/29/2020', 'mm/dd/yyyy'), 10, 35, 150 from dual union all
select to_date('3/31/2020', 'mm/dd/yyyy'), 10, 40, 300 from dual union all
select to_date('4/1/2020', 'mm/dd/yyyy'), 1, 3, 61 from dual union all
select to_date('4/2/2020', 'mm/dd/yyyy'), 5, 9, 08 from dual union all
select to_date('4/7/2020', 'mm/dd/yyyy'), 4, 8, 11 from dual
)
, prep as (
select MonthEnd
, case when trunc(sysdate, 'month') = trunc(monthend, 'month') then trunc(sysdate, 'month') end grp_dt
, NumColA, NumColB, NumColC
from t
)
select txt, sa, sb, sc from (
select grouping_id (monthend, grp_dt) grp, monthend, grp_dt
, case when grouping_id (monthend, grp_dt) = 2 and grp_dt is not null then 'MTD'
when grouping_id (monthend, grp_dt) = 2 and grp_dt is null then 'Prev'
when grouping_id (monthend, grp_dt) = 3 then 'Grand Total'
else to_char(monthend, 'mm/dd/yyyy') end txt
, sum(NumColA) sa, sum(numcolb) sb, sum(numcolc) sc
from prep
group by grouping sets((monthend, grp_dt), (grp_dt), ())
)
where txt != 'Prev'
order by grp, monthend;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.