统计人数---在线等啊

luo7269315 2010-06-09 03:31:10
我有一个职位表(cmsmv)
员工编号 部门 入职时间 离职时间(离职空值即没有请辞)
123 00 20050203 20070101
124 00 20071231 20080504
125 00 20070131 20070805
126 00 20070205
127 01 20080304 20090101
。。。
我要得到的查询结果是
部门 年份 1月份 2月份 3月份 ... 年总人数
00 2007 0 2 2 ... 2
00 2008 2 2 3 ... 3
00 2009 1 1 1 ... 1
01 2007 0 2 2 ... 2
。。。。。
根据入职时间和离职时间统计每个月和每一年的在职员工人数
条件:入职时间如果为20071231则不计入07年,离职时间为20080101则不计入08年数据
我现在要统计07---09年之间的数据。
在线等啊。。。我应该如何写条件啊??头大。。。。泪奔。。。

...全文
224 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
luoyoumou 2010-06-09
  • 打赏
  • 举报
回复
-- 修正一下:所有的“离职时间”字段,加上isnull()函数

CREATE TABLE cmsmv(
员工编号 INT,
部门 varchar(10),
入职时间 datetime,
离职时间 datetime
);


select * from cmsmv;

insert into cmsmv(员工编号,部门,入职时间,离职时间) values(123,'00','20050203','20070101');
insert into cmsmv(员工编号,部门,入职时间,离职时间) values(124,'00','20071231','20080504');
insert into cmsmv(员工编号,部门,入职时间,离职时间) values(125,'00','20070131','20070805');
insert into cmsmv(员工编号,部门,入职时间,离职时间) values(126,'00','20070205',null);
insert into cmsmv(员工编号,部门,入职时间,离职时间) values(127,'01','20080304','20090101');

select * from cmsmv;

SELECT c1.部门, t1.years as '年份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-01'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-01'
and day(isnull(c1.离职时间,(case when day(getdate())=1 then getdate()+1 else getdate() end)))<>1 -- 考虑你的特殊情况
then 1 else 0 end ) AS '1月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-02'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-02'
then 1 else 0 end ) AS '2月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-03'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-03'
then 1 else 0 end ) AS '3月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-04'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-04'
then 1 else 0 end ) AS '4月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-05'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-05'
then 1 else 0 end ) AS '5月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-06'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-06'
then 1 else 0 end ) AS '6月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-07'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-07'
then 1 else 0 end ) AS '7月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-08'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-08'
then 1 else 0 end ) AS '8月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-09'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-09'
then 1 else 0 end ) AS '9月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-10'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-10'
then 1 else 0 end ) AS '10月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-11'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-11'
then 1 else 0 end ) AS '11月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-12'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-12'
and day(isnull(c1.离职时间,(case when day(getdate())=31 then getdate()+1 else getdate() end)))<>31 -- 考虑你的特殊情况
then 1 else 0 end ) AS '12月份'
FROM cmsmv c1 join (select 2007 as years union all select 2008 union all select 2009 ) t1
on t1.years >= year(c1.入职时间)
and t1.years <= year(isnull(c1.离职时间,getdate()))
group by c1.部门, t1.years
order by t1.years, c1.部门;
luo7269315 2010-06-09
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 luoyoumou 的回复:]
引用 14 楼 luo7269315 的回复:
柔柔问一句。。。可以通过运行吗??


-- 靠,花了这么大的精力,你测试一下也赖得吗?
[/Quote]

呵呵。。。俺脸红啊。。。
luo7269315 2010-06-09
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 luoyoumou 的回复:]
00 2007 2 2 2 2 2 2 2 2 1 1 1 2
00 2008 2 2 2 2 2 1 1 1 1 1 1 1
01 2008 0 0 1 1 1 1 1 1 1 1 1 1
00 2009 1 1 1 1 1 1 1 1 1 1 1 1
01 2009 1 0 0 0 0 0 0 0 0 0 0 0
[/Quote]
我看了你的数据。。。00部门07年1月份应该是0人的。。。。不过看了你的提示。。。俺找到思路了。。谢谢
luoyoumou 2010-06-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 luo7269315 的回复:]
柔柔问一句。。。可以通过运行吗??
[/Quote]

-- 用我13楼的代码测试一下! 见过赖的,就没见过你这样的!
luoyoumou 2010-06-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 luo7269315 的回复:]
柔柔问一句。。。可以通过运行吗??
[/Quote]

-- 靠,花了这么大的精力,你测试一下也赖得吗?
luoyoumou 2010-06-09
  • 打赏
  • 举报
回复
00 2007 2 2 2 2 2 2 2 2 1 1 1 2
00 2008 2 2 2 2 2 1 1 1 1 1 1 1
01 2008 0 0 1 1 1 1 1 1 1 1 1 1
00 2009 1 1 1 1 1 1 1 1 1 1 1 1
01 2009 1 0 0 0 0 0 0 0 0 0 0 0
luo7269315 2010-06-09
  • 打赏
  • 举报
回复
柔柔问一句。。。可以通过运行吗??
luoyoumou 2010-06-09
  • 打赏
  • 举报
回复
CREATE TABLE cmsmv(
员工编号 INT,
部门 varchar(10),
入职时间 datetime,
离职时间 datetime
);


select * from cmsmv;

insert into cmsmv(员工编号,部门,入职时间,离职时间) values(123,'00','20050203','20070101');
insert into cmsmv(员工编号,部门,入职时间,离职时间) values(124,'00','20071231','20080504');
insert into cmsmv(员工编号,部门,入职时间,离职时间) values(125,'00','20070131','20070805');
insert into cmsmv(员工编号,部门,入职时间,离职时间) values(126,'00','20070205',null);
insert into cmsmv(员工编号,部门,入职时间,离职时间) values(127,'01','20080304','20090101');

select * from cmsmv;

SELECT c1.部门, t1.years as '年份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-01'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-01'
and day(isnull(c1.离职时间,(case when day(getdate())=1 then getdate()+1 else getdate() end)))<>1 -- 考虑你的特殊情况
then 1 else 0 end ) AS '1月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-02'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-02'
then 1 else 0 end ) AS '2月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-03'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-03'
then 1 else 0 end ) AS '3月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-04'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-04'
then 1 else 0 end ) AS '4月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-05'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-05'
then 1 else 0 end ) AS '5月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-06'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-06'
then 1 else 0 end ) AS '6月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-07'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-07'
then 1 else 0 end ) AS '7月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-08'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-08'
then 1 else 0 end ) AS '8月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-09'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-09'
then 1 else 0 end ) AS '9月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-10'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-10'
then 1 else 0 end ) AS '10月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-11'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-11'
then 1 else 0 end ) AS '11月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-12'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-12'
and day(isnull(c1.离职时间,(case when day(getdate())=31 then getdate()+1 else getdate() end)))<>31 -- 考虑你的特殊情况
then 1 else 0 end ) AS '12月份'
FROM cmsmv c1 join (select 2007 as years union all select 2008 union all select 2009 ) t1
on t1.years >= year(isnull(c1.入职时间,getdate()))
and t1.years <= year(isnull(c1.离职时间,getdate()))
group by c1.部门, t1.years
order by t1.years, c1.部门;
luoyoumou 2010-06-09
  • 打赏
  • 举报
回复
CREATE TABLE cmsmv(
员工编号 INT,
部门 varchar(10),
入职时间 datetime,
离职时间 datetime
);


select * from cmsmv;

insert into cmsmv(员工编号,部门,入职时间,离职时间) values(123,'00','20050203','20070101');
insert into cmsmv(员工编号,部门,入职时间,离职时间) values(124,'00','20071231','20080504');
insert into cmsmv(员工编号,部门,入职时间,离职时间) values(125,'00','20070131','20070805');
insert into cmsmv(员工编号,部门,入职时间,离职时间) values(126,'00','20070205',null);
insert into cmsmv(员工编号,部门,入职时间,离职时间) values(127,'01','20080304','20090101');

select * from cmsmv;

SELECT c1.部门, t1.years as '年份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-01'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-01'
then 1 else 0 end ) AS '1月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-02'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-02'
then 1 else 0 end ) AS '2月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-03'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-03'
then 1 else 0 end ) AS '3月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-04'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-04'
then 1 else 0 end ) AS '4月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-05'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-05'
then 1 else 0 end ) AS '5月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-06'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-06'
then 1 else 0 end ) AS '6月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-07'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-07'
then 1 else 0 end ) AS '7月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-08'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-08'
then 1 else 0 end ) AS '8月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-09'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-09'
then 1 else 0 end ) AS '9月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-10'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-10'
then 1 else 0 end ) AS '10月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-11'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-11'
then 1 else 0 end ) AS '11月份',
sum(case when convert(varchar(7),c1.入职时间,120)<=convert(varchar(7),t1.years)+'-12'
and convert(varchar(7),isnull(c1.离职时间,getdate()),120)>=convert(varchar(7),t1.years)+'-12'
then 1 else 0 end ) AS '12月份'
FROM cmsmv c1 join (select 2007 as years union all select 2008 union all select 2009 ) t1
on t1.years >= year(isnull(c1.入职时间,getdate()))
and t1.years <= year(isnull(c1.离职时间,getdate()))
group by c1.部门, t1.years
order by t1.years, c1.部门;
luo7269315 2010-06-09
  • 打赏
  • 举报
回复
有没有可以依据统计的??因为我还要横向现实月份的。。。郁闷啊。。。。
luo7269315 2010-06-09
  • 打赏
  • 举报
回复

--07年
SELECT LEFT(部门,3) AS '部门',(case LEFT(入职时间,4) WHEN '2007' THEN '2007' ELSE '2007' END) AS '07年',COUNT(MV001) AS '人数' FROM CMSMV
WHERE 入职时间< '20071231' AND (离职时间 > '20070101' OR 离职时间='' ) AND 离职时间<>入职时间
GROUP BY LEFT(入职时间,4),LEFT(部门,3)
ORDER BY LEFT(入职时间,4),LEFT(部门,3)
--08年
SELECT LEFT(部门,3) AS '部门',(case LEFT(入职时间,4) WHEN '2008' THEN '2008' ELSE '2008' END) AS '08年',COUNT(MV001) AS '人数' FROM CMSMV
WHERE 入职时间< '20081231' AND (离职时间 > '20080101' OR 离职时间='' ) AND 离职时间<>入职时间
GROUP BY LEFT(入职时间,4),LEFT(部门,3)
ORDER BY LEFT(入职时间,4),LEFT(部门,3)
--09年
SELECT LEFT(部门,3) AS '部门',(case LEFT(入职时间,4) WHEN '2009' THEN '2009' ELSE '2009' END) AS '09年',COUNT(MV001) AS '人数' FROM CMSMV
WHERE 入职时间< '20091231' AND (离职时间 > '20090101' OR 离职时间='' ) AND 离职时间<>入职时间
GROUP BY LEFT(入职时间,4),LEFT(部门,3)
ORDER BY LEFT(入职时间,4),LEFT(部门,3)

luo7269315 2010-06-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lcqtgb 的回复:]
select a.年,a.月,a.员工数-b.员工数
(select datepart(yyyy,入职时间) 年,datepart(mm,入职时间)月,count(员工编号)员工数 from TB
where datepart(yyyy,入职时间) between '2007' and '2009'
and 离职时间<>''
group by datepart(yyyy,入职时间),dat……
[/Quote]

一条语句就可以解决吗??
我写了三条的。。。。有没有一条就可以解决的?
还是觉得你的有一点点问题。。。如果我的入职时间为1999年呢?那样就进计算不了啦。。。
lcqtgb 2010-06-09
  • 打赏
  • 举报
回复
不对了。。。不写了。。看懂了。。。要对日期相减才行。。
lcqtgb 2010-06-09
  • 打赏
  • 举报
回复
select a.年,a.月,a.员工数-b.员工数
(select datepart(yyyy,入职时间) 年,datepart(mm,入职时间)月,count(员工编号)员工数 from TB
where datepart(yyyy,入职时间) between '2007' and '2009'
and 离职时间<>''
group by datepart(yyyy,入职时间),datepart(mm,入职时间))a,(select datepart(yyyy,入职时间),datepart(mm,入职时间),count(员工编号) from tb where left(入职时间,4)='1231' and 离职时间<>''
group by datepart(yyyy,入职时间) between '2007' and '2009')b
where a.datepart(yyyy,入职时间)=b.datepart(yyyy,入职时间) and a.datepart(mm,入职时间)=b.datepart(mm,入职时间)

luo7269315 2010-06-09
  • 打赏
  • 举报
回复
统计在职人数。。。不过只要这个人在这一年做了一天时间以上都要+上去
luo7269315 2010-06-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lcqtgb 的回复:]
select datepart(yyyy,入职时间),datepart(mm,入职时间),count(员工编号) from TB
where datepart(yyyy,入职时间) between '2007' and '2009'
and 离职时间<>''
group by datepart(yyyy,离职时间),datepart(mm,入职时间)

不知所谓。。先这样写写。。
[/Quote]
。。。你查出来的数据肯定错误!!!
lcqtgb 2010-06-09
  • 打赏
  • 举报
回复
是统计在职还是离职。我对你的需求有点晕。。
lcqtgb 2010-06-09
  • 打赏
  • 举报
回复
select datepart(yyyy,入职时间),datepart(mm,入职时间),count(员工编号) from TB
where datepart(yyyy,入职时间) between '2007' and '2009'
and 离职时间<>''
group by datepart(yyyy,离职时间),datepart(mm,入职时间)

不知所谓。。先这样写写。。
jaydom 2010-06-09
  • 打赏
  • 举报
回复
得慢慢看啊
luo7269315 2010-06-09
  • 打赏
  • 举报
回复
么人???自己sf

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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