22,299
社区成员




select a.departname 部门名称,b.cnts 受理业务,c.cnt 出证
from T_Department a,(select departid,count(1)cnts from T_Business group by DEPARTID)b,
(select departid,cnt=count(1) from T_Business where position='12' group by DEPARTID)c
where a.ID=b.departid and a.id=c.departid
declare @T_Business table (DepartId varchar(9),Position varchar(2),Id varchar(9))
declare @T_Department table(Id varchar(9),Departname varchar(6))
insert into @T_Business
select '007514637', '12', '080131002' union all
select '00751440x', '03', '080131003' union all
select '00751440x', '12', '080131004' union all
select '00751440x', '12', '080131001'
insert into @T_Department
select '007514637', '文管局' union all
select '00751440x', '交通局'
select a.departname 部门名称,b.cnts 受理业务,c.cnt 出证
from @T_Department a,(select departid,count(1)cnts from @T_Business group by DEPARTID)b,
(select departid,cnt=count(1) from @T_Business where position='12' group by DEPARTID)c
where a.ID=b.departid and a.id=c.departid
SELECT D.NAME, COUNT(*) AS 受理业务,(SELECT COUNT(*) FROM T_BUSINESS INNER JOIN T_DEPARTMENT ON
T_BUSINESS.DEPARTID=T_DEPARTMENT.ID WHERE LASTPOSITIONID=12 and DEPARTNAME = '某个局') AS 出证
FROM T_BUSINESS B INNER JOIN T_DEPARTMENT D ON B.DEPARTID=D.ID and DEPARTNAME = '某个局'
GROUP BY D.ID,D.NAME
select b.DEPARTNAME ,count(1) as 受理业务,
sum(case POSITION when '12' then 1 else 0 end) as 出证
from T_BUSINESS a inner join T_DEPARTMENT b on a.DEPARTID = b.ID
group by b.ID,b.DEPARTNAME
declare @T_Business table (DepartId varchar(9),Position varchar(2),Id varchar(9))
declare @T_Department table(Id varchar(9),Departname varchar(6))
insert into @T_Business
select '007514637', '12', '080131002' union all
select '00751440x', '03', '080131003' union all
select '00751440x', '12', '080131004' union all
select '00751440x', '12', '080131001'
insert into @T_Department
select '007514637', '文管局' union all
select '00751440x', '交通局'
select b.DEPARTNAME as 部门名称,count(1) as 受理业务,
sum(case POSITION when '12' then 1 else 0 end) as 出证
from @T_BUSINESS a
right join @T_DEPARTMENT b on a.DEPARTID = b.ID
group by b.ID,b.DEPARTNAME
/*
部门名称 受理业务 出证
------ ----------- -----------
交通局 3 2
文管局 1 1
(所影响的行数为 2 行)
*/
declare @T_Business table (DepartId varchar(9),Position varchar(2),Id varchar(9))
declare @T_Department table(Id varchar(9),Departname varchar(6))
insert into @T_Business
select '007514637', '12', '080131002' union all
select '00751440x', '03', '080131003' union all
select '00751440x', '12', '080131004' union all
select '00751440x', '12', '080131001'
insert into @T_Department
select '007514637', '文管局' union all
select '00751440x', '交通局'
select * from @T_Business
select * from @T_Department
select a.Departname,b.受理业务,c.出证
from @T_Department a,
(select departid ,count(position) as 受理业务 from @T_Business group by departid) b,
(select departid ,count(position) as 出证 from @T_Business where position ='12' group by departid)c
where a.id=b.departid and b.departid*=c.departid
/*
(所影响的行数为 4 行)
(所影响的行数为 2 行)
DepartId Position Id
--------- -------- ---------
007514637 12 080131002
00751440x 03 080131003
00751440x 12 080131004
00751440x 12 080131001
(所影响的行数为 4 行)
Id Departname
--------- ----------
007514637 文管局
00751440x 交通局
(所影响的行数为 2 行)
Departname 受理业务 出证
---------- ----------- -----------
交通局 3 2
文管局 1 1
(所影响的行数为 2 行)
*/
select b.DEPARTNAME ,count(1) as 受理业务,
sum(case POSITION when '12' then 1 else 0 end) as 出证
from T_BUSINESS a inner join T_DEPARTMENT b on a.DEPARTID = b.ID
group by b.ID,b.DEPARTNAME
select a.departname 部门名称,b.cnts 受理业务,c.cnt 出证
from T_DEPARTMENT a,(select departid,count(1)cnts from T_BUSINESS group by DEPARTID)b,
(select departid,cnt=sum(case position when '12' then 1 else 0 end) from T_BUSINESS)c
where a.ID=b.departid and a.id=c.departid