create table tb(部门编号 varchar(20),部门名称 varchar(20)) goinsert into tb select '000A','行政部门' insert into tb select '000B','财务部门' insert into tb select '000C','市场部门' insert into tb select '000D','后勤部门'create table salary(员工编号 varchar(20),发放日期 varchar(20), 工资数 numeric(9,2), 是否审批 varchar(20)) go insert into salary select '000A0001','20110101',2000,'己审批' insert into salary select '000A0001','20110201',2000,'己审批' insert into salary select '000A0002','20110101',2000,'己审批' insert into salary select '000A0002','20110201',2000,'未审批' insert into salary select '000B0001','20110201',2000,'己审批' select t2.部门编号,count(*) cnt from salary t1 left join tb t2 on left(t1.员工编号,4)=t2.部门编号 where 是否审批='己审批' group by t2.部门编号 --- 部门编号 cnt -------------------- ----------- 000A 3 000B 1(2 行受影响)
部门总数:select count(SUBSTRING(员工编码,1,4)),SUBSTRING(员工编码,1,4)) from 工资表 where 员工编码 not in (select 员工编码 from 工资表 where 是否审批='未审批') group by count(SUBSTRING(员工编码,1,4)) 如果要部门名字的话,就需要连接第一个表了,那应该难不倒你
select t2.部门编号,t2.部门名称,count(*) cnt from salary t1 left join tb t2 on left(t1.员工编号,4)=t2.部门编号 where 是否审批='己审批' group by t2.部门编号,t2.部门名称 order by t2.部门编号 ----------- 部门编号 部门名称 cnt -------------------- -------------------- ----------- 000A 行政部门 3 000B 财务部门 1(2 行受影响)
--生成测试数据 if object_id('[tb1]') is not null drop table [tb1] create table [tb1] (部门编码 varchar(4),部门名称 varchar(8)) insert into [tb1] select '000A','行政部门' union all select '000B','财务部门' union all select '000C','市场部门' union all select '000D','后勤部门'if object_id('[tb2]') is not null drop table [tb2] create table [tb2] (员工编码 varchar(8),发放日期 datetime,工资数 numeric(6,2),是否审批 varchar(6)) insert into [tb2] select '000A0001','20110101',1000.00,'已审批' union all select '000A0001','20110201',1000.00,'已审批' union all select '000A0001','20110301',1000.00,'已审批' union all select '000A0002','20110101',1100.00,'已审批' union all select '000A0002','20110201',1100.00,'已审批' union all select '000A0002','20110301',1100.00,'已审批' union all select '000B0001','20110101',1000.00,'已审批' union all select '000B0001','20110201',1000.00,'已审批' union all select '000B0001','20110301',1000.00,'已审批' union all select '000B0002','20110101',1100.00,'已审批' union all select '000B0002','20110201',1100.00,'已审批' union all select '000B0002','20110301',1100.00,'未审批' union all select '000C0001','20110101',1000.00,'未审批' union all select '000C0001','20110201',1000.00,'未审批' union all select '000C0001','20110301',1000.00,'未审批' union all select '000C0002','20110101',1100.00,'未审批' union all select '000C0002','20110201',1100.00,'未审批' union all select '000C0002','20110301',1100.00,'未审批'--开始查询 select * from ( select distinct 部门编码,部门名称 from tb1 a join tb2 b on a.部门编码=left(b.员工编码,4) ) t where not exists(select 1 from tb2 where left(员工编码,4)=t.部门编码 and 是否审批='未审批')--结束查询 drop table [tb1],[tb2]/* 部门编码 部门名称 ---- -------- 000A 行政部门
create table tb1(部门编码 varchar(20),部门名称 varchar(20)) insert into tb1 select '000A','行政部门' insert into tb1 select '000B','财务部门' insert into tb1 select '000C','市场部门' insert into tb1 select '000D','后勤部门' create table tb2(员工编码 varchar(20),发放日期 varchar(20), 工资数 numeric(9,2), 是否审批 varchar(20)) insert into tb2 select '000A0001','20110101',1000.00,'已审批' union all select '000A0001','20110201',1000.00,'已审批' union all select '000A0001','20110301',1000.00,'已审批' union all select '000A0002','20110101',1100.00,'已审批' union all select '000A0002','20110201',1100.00,'已审批' union all select '000A0002','20110301',1100.00,'已审批' union all select '000B0001','20110101',1000.00,'已审批' union all select '000B0001','20110201',1000.00,'已审批' union all select '000B0001','20110301',1000.00,'已审批' union all select '000B0002','20110101',1100.00,'已审批' union all select '000B0002','20110201',1100.00,'已审批' union all select '000B0002','20110301',1100.00,'未审批' union all select '000C0001','20110101',1000.00,'未审批' union all select '000C0001','20110201',1000.00,'未审批' union all select '000C0001','20110301',1000.00,'未审批' union all select '000C0002','20110101',1100.00,'未审批' union all select '000C0002','20110201',1100.00,'未审批' union all select '000C0002','20110301',1100.00,'未审批'select m.* from tb1 m where 部门编码 not in (select distinct left(员工编码,4) 部门编码 from tb2 where 是否审批 <> '已审批') and exists (select 1 from tb2 where left(员工编码,4) = m.部门编码)drop table tb1 , tb2/* 部门编码 部门名称 -------------------- -------------------- 000A 行政部门(所影响的行数为 1 行) */
select a.* from tb1 a where not eixsts (select 1 from tb2 where 部门编码=left(员工编码,4) and 是否审批 <> '已审批') and exists (select 1 from tb2 where left(员工编码,4) = a.部门编码)
create table tb(部门编号 varchar(20),部门名称 varchar(20))
goinsert into tb select '000A','行政部门'
insert into tb select '000B','财务部门'
insert into tb select '000C','市场部门'
insert into tb select '000D','后勤部门'create table salary(员工编号 varchar(20),发放日期 varchar(20), 工资数 numeric(9,2), 是否审批 varchar(20))
go
insert into salary select '000A0001','20110101',2000,'己审批'
insert into salary select '000A0001','20110201',2000,'己审批'
insert into salary select '000A0002','20110101',2000,'己审批'
insert into salary select '000A0002','20110201',2000,'未审批'
insert into salary select '000B0001','20110201',2000,'己审批'
select t2.部门编号,count(*) cnt
from salary t1
left join tb t2
on left(t1.员工编号,4)=t2.部门编号
where 是否审批='己审批'
group by t2.部门编号
---
部门编号 cnt
-------------------- -----------
000A 3
000B 1(2 行受影响)
from 工资表
where 员工编码 not in
(select 员工编码 from 工资表 where 是否审批='未审批')
group by count(SUBSTRING(员工编码,1,4))
如果要部门名字的话,就需要连接第一个表了,那应该难不倒你
from salary t1
left join tb t2
on left(t1.员工编号,4)=t2.部门编号
where 是否审批='己审批'
group by t2.部门编号,t2.部门名称
order by t2.部门编号
-----------
部门编号 部门名称 cnt
-------------------- -------------------- -----------
000A 行政部门 3
000B 财务部门 1(2 行受影响)
--生成测试数据
if object_id('[tb1]') is not null drop table [tb1]
create table [tb1] (部门编码 varchar(4),部门名称 varchar(8))
insert into [tb1]
select '000A','行政部门' union all
select '000B','财务部门' union all
select '000C','市场部门' union all
select '000D','后勤部门'if object_id('[tb2]') is not null drop table [tb2]
create table [tb2] (员工编码 varchar(8),发放日期 datetime,工资数 numeric(6,2),是否审批 varchar(6))
insert into [tb2]
select '000A0001','20110101',1000.00,'已审批' union all
select '000A0001','20110201',1000.00,'已审批' union all
select '000A0001','20110301',1000.00,'已审批' union all
select '000A0002','20110101',1100.00,'已审批' union all
select '000A0002','20110201',1100.00,'已审批' union all
select '000A0002','20110301',1100.00,'已审批' union all
select '000B0001','20110101',1000.00,'已审批' union all
select '000B0001','20110201',1000.00,'已审批' union all
select '000B0001','20110301',1000.00,'已审批' union all
select '000B0002','20110101',1100.00,'已审批' union all
select '000B0002','20110201',1100.00,'已审批' union all
select '000B0002','20110301',1100.00,'未审批' union all
select '000C0001','20110101',1000.00,'未审批' union all
select '000C0001','20110201',1000.00,'未审批' union all
select '000C0001','20110301',1000.00,'未审批' union all
select '000C0002','20110101',1100.00,'未审批' union all
select '000C0002','20110201',1100.00,'未审批' union all
select '000C0002','20110301',1100.00,'未审批'--开始查询
select * from (
select distinct 部门编码,部门名称 from tb1 a join tb2 b on a.部门编码=left(b.员工编码,4)
) t
where not exists(select 1 from tb2 where left(员工编码,4)=t.部门编码 and 是否审批='未审批')--结束查询
drop table [tb1],[tb2]/*
部门编码 部门名称
---- --------
000A 行政部门
insert into tb1 select '000A','行政部门'
insert into tb1 select '000B','财务部门'
insert into tb1 select '000C','市场部门'
insert into tb1 select '000D','后勤部门'
create table tb2(员工编码 varchar(20),发放日期 varchar(20), 工资数 numeric(9,2), 是否审批 varchar(20))
insert into tb2
select '000A0001','20110101',1000.00,'已审批' union all
select '000A0001','20110201',1000.00,'已审批' union all
select '000A0001','20110301',1000.00,'已审批' union all
select '000A0002','20110101',1100.00,'已审批' union all
select '000A0002','20110201',1100.00,'已审批' union all
select '000A0002','20110301',1100.00,'已审批' union all
select '000B0001','20110101',1000.00,'已审批' union all
select '000B0001','20110201',1000.00,'已审批' union all
select '000B0001','20110301',1000.00,'已审批' union all
select '000B0002','20110101',1100.00,'已审批' union all
select '000B0002','20110201',1100.00,'已审批' union all
select '000B0002','20110301',1100.00,'未审批' union all
select '000C0001','20110101',1000.00,'未审批' union all
select '000C0001','20110201',1000.00,'未审批' union all
select '000C0001','20110301',1000.00,'未审批' union all
select '000C0002','20110101',1100.00,'未审批' union all
select '000C0002','20110201',1100.00,'未审批' union all
select '000C0002','20110301',1100.00,'未审批'select m.* from tb1 m where
部门编码 not in (select distinct left(员工编码,4) 部门编码 from tb2 where 是否审批 <> '已审批') and
exists (select 1 from tb2 where left(员工编码,4) = m.部门编码)drop table tb1 , tb2/*
部门编码 部门名称
-------------------- --------------------
000A 行政部门(所影响的行数为 1 行)
*/
a.*
from
tb1 a
where
not eixsts (select 1 from tb2 where 部门编码=left(员工编码,4) and 是否审批 <> '已审批')
and
exists (select 1 from tb2 where left(员工编码,4) = a.部门编码)