原数据如下
select fines_group ,fines_type,time,fines cou
from zc_fines_report
第一步统计(这一步相信都会)按日期和想要统计的字段统计
select fines_group ,fines_type,time,sum(fines) cou
from zc_fines_report
where to_char(time,'yyyy')=2017
group by fines_group ,fines_type,time 结果如下:
接下来这一步至关重要,那就是把数据分月份统计出来(主要把时间那一栏进行处理同月份的进行处理)
select case when to_char(time,'mm') = 1 then 1
when to_char(time,'mm') = 2 then 2
when to_char(time,'mm') = 3 then 3
when to_char(time,'mm') = 4 then 4
when to_char(time,'mm') = 5 then 5
when to_char(time,'mm') = 6 then 6
when to_char(time,'mm') = 7 then 7
when to_char(time,'mm') = 8 then 8
when to_char(time,'mm') = 9 then 9
when to_char(time,'mm') = 10 then 10
when to_char(time,'mm') = 11 then 11
when to_char(time,'mm') = 12 then 12 end as m,
fines_group ,fines_type,cou from(
select fines_group ,fines_type,time,sum(fines) cou
from zc_fines_report
where to_char(time,'yyyy')=2017
group by fines_group ,fines_type,time
)
结果如下:
接下来就是把相同的月份的数据进行统计
select case when m = 1 then c1 end as m1,
case when m = 2 then c1 end as m2,
case when m = 3 then c1 end as m3,
case when m = 4 then c1 end as m4,
case when m = 5 then c1 end as m5,
case when m = 6 then c1 end as m6,
case when m = 7 then c1 end as m7,
case when m = 8 then c1 end as m8,
case when m = 9 then c1 end as m9,
case when m = 10 then c1 end as m10,
case when m = 11 then c1 end as m11,
case when m = 12 then c1 end as m12,
m,fines_group ,fines_type from(
select m,fines_group ,fines_type,sum(cou) c1 from(
select case when to_char(time,'mm') = 1 then 1
when to_char(time,'mm') = 2 then 2
when to_char(time,'mm') = 3 then 3
when to_char(time,'mm') = 4 then 4
when to_char(time,'mm') = 5 then 5
when to_char(time,'mm') = 6 then 6
when to_char(time,'mm') = 7 then 7
when to_char(time,'mm') = 8 then 8
when to_char(time,'mm') = 9 then 9
when to_char(time,'mm') = 10 then 10
when to_char(time,'mm') = 11 then 11
when to_char(time,'mm') = 12 then 12 end as m,
fines_group ,fines_type,cou from(
select fines_group ,fines_type,time,sum(fines) cou
from zc_fines_report
where to_char(time,'yyyy')=2017
group by fines_group ,fines_type,time
)
)group by m,fines_group ,fines_type
)
这个结果是不是很接近你想要的结果了
最后一步就简单了,把相同统计维度的数据整合在一个行中就完成了
select fines_group ,fines_type,sum(m1) m1,sum(m2) m2,sum(m3) m3,sum(m4) m4,sum(m5) m5,sum(m6) m6,
sum(m7) m7,sum(m8) m8,sum(m9) m9,sum(m10) m10,sum(m11) m11,sum(m12) m12,
(nvl(sum(m1),0)+nvl(sum(m2),0)+nvl(sum(m3),0)+nvl(sum(m4),0)+nvl(sum(m5),0)+nvl(sum(m6),0)+nvl(sum(m7),0)+nvl(sum(m8),0)+
nvl(sum(m9),0)+nvl(sum(m10),0)+nvl(sum(m11),0)+nvl(sum(m12),0)) couall
from(
select case when m = 1 then c1 end as m1,
case when m = 2 then c1 end as m2,
case when m = 3 then c1 end as m3,
case when m = 4 then c1 end as m4,
case when m = 5 then c1 end as m5,
case when m = 6 then c1 end as m6,
case when m = 7 then c1 end as m7,
case when m = 8 then c1 end as m8,
case when m = 9 then c1 end as m9,
case when m = 10 then c1 end as m10,
case when m = 11 then c1 end as m11,
case when m = 12 then c1 end as m12,
m,fines_group ,fines_type from(
select m,fines_group ,fines_type,sum(cou) c1 from(
select case when to_char(time,'mm') = 1 then 1
when to_char(time,'mm') = 2 then 2
when to_char(time,'mm') = 3 then 3
when to_char(time,'mm') = 4 then 4
when to_char(time,'mm') = 5 then 5
when to_char(time,'mm') = 6 then 6
when to_char(time,'mm') = 7 then 7
when to_char(time,'mm') = 8 then 8
when to_char(time,'mm') = 9 then 9
when to_char(time,'mm') = 10 then 10
when to_char(time,'mm') = 11 then 11
when to_char(time,'mm') = 12 then 12 end as m,
fines_group ,fines_type,cou from(
select fines_group ,fines_type,time,sum(fines) cou
from zc_fines_report
where to_char(time,'yyyy')=2017
group by fines_group ,fines_type,time
)
)group by m,fines_group ,fines_type
)
)group by fines_group ,fines_type;
结果如下:
这种方式适合直接在数据库中统计数据,不用在后台中麻烦处理,不过数据库写的有点多,可能水平一般,可以借鉴,也欢迎各位大神有好的方法,可以相互交流
sql没有过多优化,想了2小时的结果,不想去优化了,先写出来再看。。。