日期:2014-05-17  浏览次数:20901 次

3个字段group by,然后再统计的sql语句的问题!
 
create table Z
(
  RIQI VARCHAR2(10),
  APN VARCHAR2(60),
  ASA VARCHAR2(60),
  SHICHANG NUMBER,
  ACC_RESULT NUMBER(20,2),
  FEE_NUMBER NUMBER(8) not null
); 
insert into Z (RIQI, APN, ASA, SHICHANG, ACC_RESULT, FEE_NUMBER)
values ('2009-10-11', '16000171', '56723986', 2, 200, 100);
insert into Z (RIQI, APN, ASA, SHICHANG, ACC_RESULT, FEE_NUMBER)
values ('2009-10-12', '16000172', '68126337', 3, 300, 100);
insert into Z (RIQI, APN, ASA, SHICHANG, ACC_RESULT, FEE_NUMBER)
values ('2009-10-13', '16000173', '25881225', 2, 200, 200);
insert into Z (RIQI, APN, ASA, SHICHANG, ACC_RESULT, FEE_NUMBER)
values ('2009-10-13', '16000174', '64164232', 2, 200, 200);
insert into Z (RIQI, APN, ASA, SHICHANG, ACC_RESULT, FEE_NUMBER)
values ('2009-10-14', '16000175', '69177818', 8, 800, 200);
insert into Z (RIQI, APN, ASA, SHICHANG, ACC_RESULT, FEE_NUMBER)
values ('2009-10-14', '16000176', '63304666', 3, 300, 200);
insert into Z (RIQI, APN, ASA, SHICHANG, ACC_RESULT, FEE_NUMBER)
values ('2009-10-13', '16000177', '62092489', 5, 500, 100);
insert into Z (RIQI, APN, ASA, SHICHANG, ACC_RESULT, FEE_NUMBER)
values ('2009-10-13', '16000178', '65188457', 3, 300, 100);
insert into Z (RIQI, APN, ASA, SHICHANG, ACC_RESULT, FEE_NUMBER)
values ('2009-10-15', '16000176', '64168782', 1, 100, 300);
insert into Z (RIQI, APN, ASA, SHICHANG, ACC_RESULT, FEE_NUMBER)
values ('2009-10-15', '16000176', '53027205', 1, 100, 300);
insert into Z (RIQI, APN, ASA, SHICHANG, ACC_RESULT, FEE_NUMBER)
values ('2009-10-15', '16000176', '64212807', 3, 300, 300);
insert into Z (RIQI, APN, ASA, SHICHANG, ACC_RESULT, FEE_NUMBER)
values ('2009-10-13', '16000176', '64364641', 2, 200, 300);
insert into Z (RIQI, APN, ASA, SHICHANG, ACC_RESULT, FEE_NUMBER)
values ('2009-10-16', '16000179', '32313222', 1, 100, 100);
insert into Z (RIQI, APN, ASA, SHICHANG, ACC_RESULT, FEE_NUMBER)
values ('2009-10-17', '16000179', '53524520', 3, 300, 100);
commit;
测试数据如上所示
我想得到如下数据,建附件图片


我自己的sql如下:
 select z.riqi,z.apn,z.fee_number,sum(z.acc_result),count(*) num 
 from z 
 group by z.fee_number ,z.riqi,z.apn
 order by z.fee_number,z.riqi,z.apn 
但是却不能按照fee_number再进行统计啦,各位有什么高招吗?做个样列sql吧,谢谢了!


------解决方案--------------------
啥意思》?这样吗?

select z.riqi,z.apn,z.fee_number,sum(z.fee_number) over (partition by z.fee_number) a,sum(z.acc_result),count(*) num
 from z
 group by z.fee_number ,z.riqi,z.apn
 order by z.fee_number,z.riqi,z.apn

------解决方案--------------------
SQL code

select z.riqi,z.apn,z.fee_number,sum(z.acc_result),count(*) num  
 from z  
 group by Rollup (z.fee_number ,z.riqi,z.apn)
 order by z.fee_number,z.riqi,z.apn

------解决方案--------------------
SQL code
 select * from (
      select z.riqi,z.apn,z.fee_number,sum(z.acc_result),count(*) num  
       from z  
       group by rollup(z.fee_number ,z.riqi,z.apn)
       order by z.fee_number,z.riqi,z.apn 
   )where (riqi is  null and apn is  null) or (riqi is not null and apn is not null)

------解决方案--------------------
SQL code
RIQI    APN    FEE_NUMBER    SUM(Z.ACC_RESULT)    NUM
2009-10-11    16000171    100    200    1
2009-10-12    16000172    100    300    1
2009-10-13    16000177    100    500    1
2009-10-13    16000178    100    300    1
2009-10-16    16000179    100    100    1
2009-10-17    16000179    100    300    1
                        100    1700    6
2009-10-13    16000173    200    200