按条件分类汇总sql脚本例子
2007-11-29 16:17
1341 查看
------按条件分类汇总sql脚本例子
select 上级部门,部门名称,姓名,
sum(case when NumType='4' then 1 else 0 end ) 新装 ,
sum(case when NumType='7' then 1 else 0 end ) 续费,
sum(case when NumType='82' then 1 else 0 end ) e8--(含单独办理e8)
from viewall --揽装视图,其实质内容为number与部门信息的映射
where statTime>='20070101'
and statTime<='20071031'
group by 上级部门,部门名称,姓名 --按字段(或字段组合)分类汇总
order by 上级部门,部门名称,姓名 --按顺序排序,如果按倒序排序 在字段后加 desc
---效果等同于下面的语句组合,下面的语句组合也是常用的
------begin---------------------------------
select 上级部门,部门名称,姓名,
sum(case when NumType='4' then 1 else 0 end ) 新装
into #t1
from viewall
where statTime>='20070101'
and statTime<='20071031'
group by 上级部门,部门名称,姓名
order by 上级部门,部门名称,姓名
select 上级部门,部门名称,姓名,
sum(case when NumType='7' then 1 else 0 end ) 续费
into #t2
from viewall
where statTime>='20070101'
and statTime<='20071031'
group by 上级部门,部门名称,姓名
order by 上级部门,部门名称,姓名
select 上级部门,部门名称,姓名,
sum(case when NumType='82' then 1 else 0 end ) e8
into #t3
from viewall
where statTime>='20070101'
and statTime<='20071031'
group by 上级部门,部门名称,姓名
order by 上级部门,部门名称,姓名
select 上级部门,部门名称,姓名 into #t4 from #t1
union select 上级部门,部门名称,姓名 from #t2
union select 上级部门,部门名称,姓名 from #t3
select a.*,b.新装 into #t5 from #t4 a left join #t1 b on a.姓名=b.姓名 and a.部门名称=b.部门名称
select a.*,b.续费 into #t6 from #t5 a left join #t2 b on a.姓名=b.姓名 and a.部门名称=b.部门名称
select a.*,b.e8 into #t7 from #t6 a left join #t3 b on a.姓名=b.姓名 and a.部门名称=b.部门名称
select * from #t7
-------end----------------------------------
select 上级部门,部门名称,姓名,
sum(case when NumType='4' then 1 else 0 end ) 新装 ,
sum(case when NumType='7' then 1 else 0 end ) 续费,
sum(case when NumType='82' then 1 else 0 end ) e8--(含单独办理e8)
from viewall --揽装视图,其实质内容为number与部门信息的映射
where statTime>='20070101'
and statTime<='20071031'
group by 上级部门,部门名称,姓名 --按字段(或字段组合)分类汇总
order by 上级部门,部门名称,姓名 --按顺序排序,如果按倒序排序 在字段后加 desc
---效果等同于下面的语句组合,下面的语句组合也是常用的
------begin---------------------------------
select 上级部门,部门名称,姓名,
sum(case when NumType='4' then 1 else 0 end ) 新装
into #t1
from viewall
where statTime>='20070101'
and statTime<='20071031'
group by 上级部门,部门名称,姓名
order by 上级部门,部门名称,姓名
select 上级部门,部门名称,姓名,
sum(case when NumType='7' then 1 else 0 end ) 续费
into #t2
from viewall
where statTime>='20070101'
and statTime<='20071031'
group by 上级部门,部门名称,姓名
order by 上级部门,部门名称,姓名
select 上级部门,部门名称,姓名,
sum(case when NumType='82' then 1 else 0 end ) e8
into #t3
from viewall
where statTime>='20070101'
and statTime<='20071031'
group by 上级部门,部门名称,姓名
order by 上级部门,部门名称,姓名
select 上级部门,部门名称,姓名 into #t4 from #t1
union select 上级部门,部门名称,姓名 from #t2
union select 上级部门,部门名称,姓名 from #t3
select a.*,b.新装 into #t5 from #t4 a left join #t1 b on a.姓名=b.姓名 and a.部门名称=b.部门名称
select a.*,b.续费 into #t6 from #t5 a left join #t2 b on a.姓名=b.姓名 and a.部门名称=b.部门名称
select a.*,b.e8 into #t7 from #t6 a left join #t3 b on a.姓名=b.姓名 and a.部门名称=b.部门名称
select * from #t7
-------end----------------------------------
相关文章推荐
- sql分类汇总及Select的自增长脚本
- sql分类汇总及Select的自增长脚本
- 一些不熟悉的SQL脚本--约束条件
- 实现数据分类汇总的SQL语句
- sql查询语句_多字段分类汇总_多表合并
- 运行执行sql文件脚本的例子
- sql servser按条件分类统计
- 省市地区的二级分类SQL脚本(含数据)
- 实现数据分类汇总的SQL语句
- SQL 之Where条件与From表无关性例子
- 汇总Javascript各种判断脚本(javascript经典例子)
- UltraEdit常用脚本小例子汇总
- SQL Performance Analyzer SPA常用脚本汇总
- SQL技巧之分类汇总
- Loadrunner测试数据库性能,测试SQL语句性能的脚本例子
- Loadrunner测试数据库性能,测试SQL语句性能的脚本例子
- 经典SQL之分类汇总
- sql 视图 按where条件多个字段取一个 分类: SQL Server 2014-12-01 14:09 308人阅读 评论(0) 收藏
- 用自定义的SQL脚本创建SQL Server数据库安装例子
- SQL 分类条件统计