分组汇总统计

--分组汇总统计
if OBJECT_ID(N'A',N'U')is not null drop table A
go
set nocount on
create table A
(
nhc_id int identity(1,1) primary key not null,
nhc_xm nvarchar(100),
nhc_khrq datetime,
nhc_kqlb nvarchar(100),
nhc_kqsj int,
nhc_fwtd nvarchar(100),
nhc_fwzl nvarchar(100)
)
insert into A
select '张三', '2011-1-1', '迟到', 1, '优', '良' union all
select '张三', '2011-1-2', '请假', 4, '优', '良' union all
select '张三', '2011-1-3', '迟到', 2, '差', '优' union all
select '张三', '2011-1-4', '矿工', 4, '优', '良' union all
select '张三', '2011-1-5', '请假', 1, '良', '差' union all
select '张三', '2011-1-6', '迟到', 3, '优', '良' union all
select '张三', '2011-1-7', '矿工', 5, '优', '优'
go

select * from A

--第一种方法适用于sql2005
with cte1 as
(select row_number()over(order by getdate()) R1,MONTH(nhc_khrq) 月份,nhc_xm,nhc_kqlb,COUNT(nhc_kqlb) 次数,SUM(nhc_kqsj) as 时长 from A group by nhc_xm,nhc_kqlb,MONTH(nhc_khrq)),cte2 as
(select row_number()over(order by nhc_fwtd) R1,nhc_xm,nhc_fwtd,count(nhc_fwtd) as V2 from A group by nhc_xm,nhc_fwtd,MONTH(nhc_khrq) ),cte3 as
(select  row_number()over(order by nhc_fwzl) R3,nhc_xm,nhc_fwzl,count(nhc_fwzl) as V2 from A group by nhc_xm,nhc_fwzl,MONTH(nhc_khrq))
select cte1.R1,cte1.月份,cte1.nhc_xm,cte1.nhc_kqlb,cte1.次数,cte1.时长,cte2.nhc_fwtd as 标准,cte2.V2 as 服务态度,cte3.V2 as 服务质量
 from cte1 inner join cte2 on cte1.R1=cte2.R1 inner join cte3 on cte2.R1=cte3.R3

--下面二、三种方法适用于sql2000/2005
if exists (select * from sysobjects where name='tb')
drop table tb
GO
create table tb(nhc_id int,nhc_xm varchar(10) , nhc_khrq varchar(10) ,
nhc_kqlb varchar(10),nhc_kqsj int,nhc_fwtd varchar(10),nhc_fwzl varchar(10),)
insert into tb values(1,'张三' , '2011-1-1' , '迟到',1,'优','良')
insert into tb values(2,'张三' , '2011-1-2' , '请假',4,'优','良')
insert into tb values(3,'张三' , '2011-1-3' , '迟到',2,'差','优')
insert into tb values(4,'张三' , '2011-1-4' , '矿工',4,'优','良')
insert into tb values(5,'张三' , '2011-1-5' , '请假',1,'良','差')
insert into tb values(6,'张三' , '2011-1-6' , '迟到',3,'优','良')
insert into tb values(7,'张三' , '2011-1-7' , '矿工',5,'优','优')
go
select * from tb

--第二种方法
select nhc_xm,'考勤' AS KQ,nhc_kqlb,count(nhc_kqsj) as kqsj  from tb
where nhc_khrq like '2011-1%'
group by nhc_xm,nhc_kqlb
union(
select nhc_xm,'服务态度' AS AT,nhc_fwtd,count(nhc_fwtd) as fwtd  from tb
where nhc_khrq like '2011-1%'
group by nhc_xm,nhc_fwtd )
union(
select nhc_xm,'服务质量' AS QA,nhc_fwzl,count(nhc_fwzl) as fwtd  from tb
where nhc_khrq like '2011-1%'
group by nhc_xm,nhc_fwzl )

--第三种方法
SELECT  nhc_xm
,CONVERT(VARCHAR(6),nhc_khrq,112) AS 月份
,sum(case when nhc_kqlb='迟到' then 1 else 0 end) as 迟到次数
,sum(case when nhc_kqlb='迟到' then nhc_kqsj else 0 end) as 迟到时长
,sum(case when nhc_kqlb='请假' then 1 else 0 end) as 请假次数
,sum(case when nhc_kqlb='请假' then nhc_kqsj else 0 end) as 请假时长
,sum(case when nhc_kqlb='矿工' then 1 else 0 end) as 矿工次数
,sum(case when nhc_fwtd='优' then 1 else 0 end) as 服务态度优次数
,sum(case when nhc_fwtd='良' then 1 else 0 end) as 服务态度良次数
,sum(case when nhc_fwtd='差' then 1 else 0 end) as 服务态度差次数
,sum(case when nhc_fwzl='优' then 1 else 0 end) as 服务质量优次数
,sum(case when nhc_fwzl='良' then 1 else 0 end) as 服务质量良次数
,sum(case when nhc_fwzl='差' then 1 else 0 end) as 服务质量差次数
FROM tb
GROUP BY nhc_xm,CONVERT(VARCHAR(6),nhc_khrq,112)

猜你喜欢

转载自blog.csdn.net/panyuanyuan/article/details/6315969