一个常用的报表统计SQL语句
更新时间:2010年12月23日 10:57:26 作者:
在平常的系统开发中,我们经常会遇到类似下图的报表统计功能需求。
一般都给定按日期区间统计某一值,每一个列就是统计时的分类。如下图:
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 后面也要同时带上增加的自选字段。

下面给出上图的例子的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 后面也要同时带上增加的自选字段。
相关文章
Centos 7.3下SQL Server安装配置方法图文教程
这篇文章主要为大家详细介绍了Centos 7.3下SQL Server安装配置方法图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-08-08
深入SQLServer中ISNULL与NULLIF的使用详解
本篇文章是对SQLServer中ISNULL与NULLIF的使用进行了详细分析介绍,需要的朋友参考下2013-06-06
SQL Server中row_number分页查询的用法详解
这篇文章主要介绍了SQL Server中row_number的用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-07-07
SQL Server 2016里的sys.dm_exec_input_buffer的问题
这篇文章主要介绍了SQL Server 2016里的sys.dm_exec_input_buffer的相关资料,需要的朋友可以参考下2016-04-04


最新评论