SQL问题5合一.

pj96127 2008-02-22 02:30:26
select count(*) as 正式 from ht_empy_dept where cpsnno in(select cpsnno from ht_empy where ht_empy.cstatus='正式' and ht_empy.dleave IS NULL and dcome <'2008-01-01') and corg='100001'

select count(*) as 试用 from ht_empy_dept where cpsnno in(select cpsnno from ht_empy where ht_empy.cstatus='试用' and ht_empy.dleave IS NULL and dcome <'2008-01-01') and corg='100001'

select count(*) as 实习 from ht_empy_dept where cpsnno in(select cpsnno from ht_empy where ht_empy.cstatus='实习' and ht_empy.dleave IS NULL and dcome <'2008-01-01') and corg='100001'

select count(*) as 临时 from ht_empy_dept where cpsnno in(select cpsnno from ht_empy where ht_empy.cstatus='临时' and ht_empy.dleave IS NULL and dcome <'2008-01-01') and corg='100001'

select count(*) as 外聘 from ht_empy_dept where cpsnno in(select cpsnno from ht_empy where ht_empy.cstatus='外聘' and ht_empy.dleave IS NULL and dcome <'2008-01-01') and corg='100001'

现在这样查每次查询都只查处一种状态的员工数,
为了提高效率,想采用存储过程实现一次查询出5种状态的员工总数.类似下图:


请教各位大哥,这个存储过程应该怎么写啊??
...全文
75 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dawugui 2008-02-22
  • 打赏
  • 举报
回复
上面已经写了这么多了?
pt1314917 2008-02-22
  • 打赏
  • 举报
回复

select
sum(case when b.cstatus='正式' then 1 else 0 end) as 正式,
sum(case when b.cstatus='试用' then 1 else 0 end) as 试用,
sum(case when b.cstatus='实习' then 1 else 0 end) as 实习,
sum(case when b.cstatus='临时' then 1 else 0 end) as 临时,
sum(case when b.cstatus='外聘' then 1 else 0 end) as 外聘
from ht_empy_dept a,ht_empy b
where b.dleave IS NULL and b.dcome<'2008-01-01' and a.corg='100001' and a.cpsnno =b.cpsnno
bqb 2008-02-22
  • 打赏
  • 举报
回复
惭愧,我上面的错了,晕!!!
-----------

select
正式=sum(case when ht_empy.cstatus='正式' then 1 else 0 end),
试用=sum(case when ht_empy.cstatus='试用' then 1 else 0 end),
实习=sum(case when ht_empy.cstatus='实习' then 1 else 0 end),
临时=sum(case when ht_empy.cstatus='临时' then 1 else 0 end),
外聘=sum(case when ht_empy.cstatus='外聘' then 1 else 0 end),
from ht_empy_dept where cpsnno in(select cpsnno from ht_empy
where ht_empy.dleave IS NULL and dcome <'2008-01-01') and corg='100001'

dawugui 2008-02-22
  • 打赏
  • 举报
回复
使用case when吧
bqb 2008-02-22
  • 打赏
  • 举报
回复
--一个语句就可以了

select
正式=count(case when ht_empy.cstatus='正式' then 1 else 0 end),
试用=count(case when ht_empy.cstatus='试用' then 1 else 0 end),
实习=count(case when ht_empy.cstatus='实习' then 1 else 0 end),
临时=count(case when ht_empy.cstatus='临时' then 1 else 0 end),
外聘=count(case when ht_empy.cstatus='外聘' then 1 else 0 end),
from ht_empy_dept where cpsnno in(select cpsnno from ht_empy
where ht_empy.dleave IS NULL and dcome <'2008-01-01') and corg='100001'


fcuandy 2008-02-22
  • 打赏
  • 举报
回复
直接group by不行?
子陌红尘 2008-02-22
  • 打赏
  • 举报
回复
select
sum(case when b.cstatus='正式' then 1 else 0 end) as 正式,
sum(case when b.cstatus='试用' then 1 else 0 end) as 试用,
sum(case when b.cstatus='实习' then 1 else 0 end) as 实习,
sum(case when b.cstatus='临时' then 1 else 0 end) as 临时,
sum(case when b.cstatus='外聘' then 1 else 0 end) as 外聘
from
ht_empy_dept a,
ht_empy b
where
a.cpsnno=b.cpsnno
and
a.corg='100001'
and
b.dleave IS NULL
and
b.dcome<'2008-01-01'
pj96127 2008-02-22
  • 打赏
  • 举报
回复
能不能写完全啊..谢谢啦.我对存储过程不是很熟练.
bqb 2008-02-22
  • 打赏
  • 举报
回复
select   
正式=count(case when ht_empy.cstatus='正式' then 1 else 0 end),
试用=count(case when ht_empy.cstatus='试用' then 1 else 0 end),
.
.
.
.
from ht_empy_dept where cpsnno in(select cpsnno from ht_empy
where ht_empy.dleave IS NULL and dcome <'2008-01-01') and corg='100001'

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧