当前位置: 代码迷 >> Sql Server >> 对查询出的结果开展统计
  详细解决方案

对查询出的结果开展统计

热度:47   发布时间:2016-04-24 18:30:49.0
对查询出的结果进行统计
这是我的查询语句 
select *from PeisPatientExamDepart
 where  (RowCreateTime>'2013-09-01' and RowCreateTime<'2013-09-30')
 order by Depart_Name_R
我想统计出Depart_Name_R字段和DepartDoctor_Name_R出现的次数并让这两个字段显示在一起,求高人解答,新人才学sql,希望各位最好能贴出代码
------解决方案--------------------
select count(1) as 次数 ,Depart_Name_R +DepartDoctor_Name from PeisPatientExamDepart
 where  (RowCreateTime>'2013-09-01' and RowCreateTime<'2013-09-30')
 order by Depart_Name_R

------解决方案--------------------
引用:

部门名   出现次数   部门员工名   出现次数
财务室     22          张三         20
财务室     22          李四         2


试试这个:
select Depart_Name_R,
       COUNT(*) over(partition by Depart_Name_R) '出现次数',
       DepartDoctor_Name_R,
       COUNT(*) over(partition by DepartDoctor_Name_R) '出现次数'
from PeisPatientExamDepart
 where  (RowCreateTime>'2013-09-01' and RowCreateTime<'2013-09-30')
order by Depart_Name_R

------解决方案--------------------
select a.epart_Name_R,a.c1,b.DepartDoctor_Name_R,b.c2 from(
select Depart_Name_R,count(*) as c1 from PeisPatientExamDepart
where  (RowCreateTime>'2013-09-01' and RowCreateTime<'2013-09-30')
group by Depart_Name_R
)a inner join(
select Depart_Name_R,DepartDoctor_Name_R,count(*) as c2 from PeisPatientExamDepart
where  (RowCreateTime>'2013-09-01' and RowCreateTime<'2013-09-30')
group by Depart_Name_R,DepartDoctor_Name_R
)b on a.Depart_Name_R=b.Depart_Name_R

------解决方案--------------------
create table PeisPatientExamDepart(Depart_Name_R nvarchar(20),DepartDoctor_Name_R nvarchar(20),RowCreateTime datetime)
insert into PeisPatientExamDepart
select '财务室','张三','2013-09-05' union all
select '财务室','张三','2013-09-08' union all
select '财务室','张三','2013-09-09' union all
select '财务室','张三','2013-09-12' union all
select '财务室','李四','2013-09-05' union all
select '财务室','李四','2013-09-11' union all
select '财务室','李四','2013-09-15'
go
select a.Depart_Name_R,a.c1,b.DepartDoctor_Name_R,b.c2 from(
select Depart_Name_R,count(*) as c1 from PeisPatientExamDepart
where  (RowCreateTime>'2013-09-01' and RowCreateTime<'2013-09-30')
group by Depart_Name_R
)a inner join(
select Depart_Name_R,DepartDoctor_Name_R,count(*) as c2 from PeisPatientExamDepart
where  (RowCreateTime>'2013-09-01' and RowCreateTime<'2013-09-30')
group by Depart_Name_R,DepartDoctor_Name_R
)b on a.Depart_Name_R=b.Depart_Name_R
/*
Depart_Name_R        c1          DepartDoctor_Name_R  c2
-------------------- ----------- -------------------- -----------
财务室                  7           李四                   3
财务室                  7           张三                   4

(2 行受影响)

*/
go
drop table PeisPatientExamDepart
  相关解决方案