sanxiatingyu

  在平常的系统开发中,我们经常会遇到类似下图的报表统计功能需求。

  一般都给定按日期区间统计某一值,每一个列就是统计时的分类。如下图:

  下面给出上图的例子的sql语句写法:(注:此处是按“日期”字段统计)

 select 日期,自选字段1,自选字段2,sum(类型1) 类型1,sum(类型2) 类型2,sum(类型3) 类型3,sum(类型4) 类型4,sum(类型5) 类型5,sum(类型6) 类型6 from (
select to_date(to_char(日期,\'yyyy-MM-dd\'),\'yyyy-mm-dd\') 日期,自选字段1,自选字段2,count(主键字段) 类型1,0 类型2,0 类型3,0 类型4,0 类型5,0 类型6 from 表名
where 1=1
group by to_date(to_char(日期,\'yyyy-MM-dd\'),\'yyyy-mm-dd\'),自选字段1,自选字段2
union all
select to_date(to_char(日期,\'yyyy-MM-dd\'),\'yyyy-mm-dd\') 日期,自选字段1,自选字段2,0 类型1,count(主键字段) 类型2,0 类型3,0 类型4,0 类型5,0 类型6 from 表名
where 1=1
group by to_date(to_char(日期,\'yyyy-MM-dd\'),\'yyyy-mm-dd\'),自选字段1,自选字段2
union all
select to_date(to_char(日期,\'yyyy-MM-dd\'),\'yyyy-mm-dd\') 日期,自选字段1,自选字段2,0 类型1,0 类型2,count(主键字段) 类型3,0 类型4,0 类型5,0 类型6 from 表名
where 1=1
group by to_date(to_char(日期,\'yyyy-MM-dd\'),\'yyyy-mm-dd\'),自选字段1,自选字段2
union all
select to_date(to_char(日期,\'yyyy-MM-dd\'),\'yyyy-mm-dd\') 日期,自选字段1,自选字段2,0 类型1,0 类型2,0 类型3,count(主键字段) 类型4,0 类型5,0 类型6 from 表名
where 1=1
group by to_date(to_char(日期,\'yyyy-MM-dd\'),\'yyyy-mm-dd\'),自选字段1,自选字段2
union all
select to_date(to_char(日期,\'yyyy-MM-dd\'),\'yyyy-mm-dd\') 日期,自选字段1,自选字段2,0 类型1,0 类型2,0 类型3,0 类型4,count(主键字段) 类型5,0 类型6 from 表名
where 1=1
group by to_date(to_char(日期,\'yyyy-MM-dd\'),\'yyyy-mm-dd\'),自选字段1,自选字段2
union all
select to_date(to_char(日期,\'yyyy-MM-dd\'),\'yyyy-mm-dd\') 日期,自选字段1,自选字段2,0 类型1,0 类型2,0 类型3,0 类型4,0 类型5,count(主键字段) 类型6 from 表名
where 1=1
group by to_date(to_char(日期,\'yyyy-MM-dd\'),\'yyyy-mm-dd\'),自选字段1,自选字段2 ) where 1=1 group by to_date(to_char(日期,\'yyyy-MM-dd\'),\'yyyy-mm-dd\'),自选字段1,自选字段2

 

这里的自选字段1,自选字段2可以自己随意增加,需要注意的是在group by 后面也要同时带上增加的自选字段。

分类:

技术点:

相关文章:

  • 2021-12-14
  • 2021-12-14
  • 2022-12-23
  • 2022-02-02
  • 2021-07-25
  • 2021-12-02
  • 2021-06-10
猜你喜欢
  • 2021-11-20
  • 2021-07-01
  • 2022-01-09
  • 2022-12-23
  • 2022-12-23
  • 2021-08-23
  • 2021-09-15
相关资源
相似解决方案