问题描述
我需要一个每月的组总计,即使查询是针对特定APPL值的,也可以汇总APPL列中的所有值。
create table test_grp_total
(dttm_month date, appl varchar2(5), mins number)
insert into test_grp_total values('01-aug-2017','AAA',100);
insert into test_grp_total values('01-aug-2017','BBB',200);
insert into test_grp_total values('01-aug-2017','CCC',100);
insert into test_grp_total values('01-aug-2017','AAA',300);
insert into test_grp_total values('01-aug-2017','BBB',500);
insert into test_grp_total values('01-aug-2017','CCC',600);
insert into test_grp_total values('01-sep-2017','AAA',200);
insert into test_grp_total values('01-sep-2017','BBB',400);
insert into test_grp_total values('01-sep-2017','CCC',600);
insert into test_grp_total values('01-sep-2017','AAA',900);
insert into test_grp_total values('01-sep-2017','BBB',600);
insert into test_grp_total values('01-sep-2017','CCC',800);
select dttm_month,appl,sum(mins) from test_grp_total group by dttm_month,appl
order by dttm_month;
select dttm_month,appl,sum(mins) from test_grp_total where appl in ( 'AAA','BBB') group by dttm_month,appl
order by dttm_month;
--I would like to see a result like this below when querying for the column aapl for values AAA,BBB
--Which analytical function is needed for this type of result and how to use them?
DTTM_MONTH APPL SUM(MINS)
8/1/2017 AAA 400
8/1/2017 BBB 700
GRAND TOTAL 1800 ---(THIS IS THE TOTAL FOR ALL APPL FOR THE MONTH)
9/1/2017 AAA 1100
9/1/2017 BBB 1000
GRAND TOTAL 3500 ---(THIS IS THE TOTAL FOR ALL APPL FOR THE MONTH)专家解答
这不是您需要添加总计行的分析: 它是汇总!
在您的group by中,汇总appl值以获取总计:
在您的group by中,汇总appl值以获取总计:
create table test_grp_total
(dttm_month date, appl varchar2(5), mins number);
insert into test_grp_total values('01-aug-2017','AAA',100);
insert into test_grp_total values('01-aug-2017','BBB',200);
insert into test_grp_total values('01-aug-2017','CCC',100);
insert into test_grp_total values('01-aug-2017','AAA',300);
insert into test_grp_total values('01-aug-2017','BBB',500);
insert into test_grp_total values('01-aug-2017','CCC',600);
insert into test_grp_total values('01-sep-2017','AAA',200);
insert into test_grp_total values('01-sep-2017','BBB',400);
insert into test_grp_total values('01-sep-2017','CCC',600);
insert into test_grp_total values('01-sep-2017','AAA',900);
insert into test_grp_total values('01-sep-2017','BBB',600);
insert into test_grp_total values('01-sep-2017','CCC',800);
select dttm_month,appl,sum(mins) from test_grp_total
group by dttm_month,rollup(appl)
order by dttm_month;
DTTM_MONTH APPL SUM(MINS)
01-AUG-2017 00:00:00 AAA 400
01-AUG-2017 00:00:00 BBB 700
01-AUG-2017 00:00:00 CCC 700
01-AUG-2017 00:00:00 1800
01-SEP-2017 00:00:00 AAA 1100
01-SEP-2017 00:00:00 BBB 1000
01-SEP-2017 00:00:00 CCC 1400
01-SEP-2017 00:00:00 3500 「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




