请教如何得到统计部门人员变化的sql文

kuailexq2000 2010-01-21 11:48:57
现在有一个统计部门人员变化的sql文
select 2008人数统计,2009人数统计 from(
select count(distinct 员工id) 2008人数统计 from 2008员工信息
left join
select count(distinct 员工id) 2009人数统计 from 2009员工信息)
现在要加入合计人数,考虑到08,09的员工id中有相同的所以不能简单的相加。
现在考虑改为如下sql
select 2008人数统计,2009人数统计,合计人数 from(
select count(distinct 员工id) 2008人数统计 from 2008员工信息
left join
select count(distinct 员工id) 2009人数统计 from 2009员工信息
left join
select count(distinct 员工id) 合计人数 from (
select distinct 员工id 2008人数统计 from 2008员工信息
union
select distinct 员工id 2009人数统计 from 2009员工信息))

因为考虑到原sql文业务复杂,关联较多参数,变更后参数部分修改容易出错。
...全文
200 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
kuailexq2000 2010-01-29
  • 打赏
  • 举报
回复
没有满意的
yyyyyhhhhwwww 2010-01-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 playwarcraft 的回复:]
SQL code--好乱啊,是这个意思??selectsum(casewhen[year]=2008then1else0end)as[2008人数统计],sum(casewhen[year]=2009then1else0end)as[2009人数统计],count(distinct 员工id)as[Total人数统计]from
(select 员工id,2008as[year]from[2008员?-
[/Quote]

写的不错
kuailexq2000 2010-01-27
  • 打赏
  • 举报
回复
up
kuailexq2000 2010-01-24
  • 打赏
  • 举报
回复
up
playwarcraft 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 happyflystone 的回复:]
晕, 不是故意抄沟沟,不过沟沟呀你每一年的重复你没有去掉
[/Quote]

用union
它会自动去掉的啦~~~~~
-狙击手- 2010-01-21
  • 打赏
  • 举报
回复
晕, 不是故意抄沟沟,不过沟沟呀你每一年的重复你没有去掉
-狙击手- 2010-01-21
  • 打赏
  • 举报
回复

select sum(case when t = '2008' then 1 else 0 end) as '2008',
sum(case when t = '2009' then 1 else 0 end) as '2009',
count(distinct id) as tot
from(
select distinct id,'2008' as t from 2008员工信息
union all
select distinct id,'2009' from 2009员工信息) a
Mr_Nice 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用楼主 kuailexq2000 的回复:]
现在有一个统计部门人员变化的sql文
select  2008人数统计,2009人数统计 from(
select count(distinct 员工id) 2008人数统计 from 2008员工信息
left join
select count(distinct 员工id) 2009人数统计 from 2009员工信息)
现在要加入合计人数,考虑到08,09的员工id中有相同的所以不能简单的相加。
现在考虑改为如下sql
select  2008人数统计,2009人数统计,合计人数 from(
select count(distinct 员工id) 2008人数统计 from 2008员工信息
left join
select count(distinct 员工id) 2009人数统计 from 2009员工信息
left join
select  count(distinct 员工id) 合计人数 from (
select distinct 员工id 2008人数统计 from 2008员工信息
union
select distinct 员工id 2009人数统计 from 2009员工信息))

因为考虑到原sql文业务复杂,关联较多参数,变更后参数部分修改容易出错。

[/Quote]


没看出思路。

lz可以先分别统计2008,2009的distinct了的值,然后再left join一下。 如果要行转列,再进行处理就行了。

参考! 路过,学习...
nianran520 2010-01-21
  • 打赏
  • 举报
回复
好乱
playwarcraft 2010-01-21
  • 打赏
  • 举报
回复

--好乱啊,是这个意思??
select sum(case when [year]=2008 then 1 else 0 end) as [2008人数统计],
sum(case when [year]=2009 then 1 else 0 end) as [2009人数统计],
count(distinct 员工id) as [Total人数统计]
from
(
select 员工id, 2008 as [year] from [2008员工信息]
union
select 员工id, 2009 as [year] from [2009员工信息]
) A
--小F-- 2010-01-21
  • 打赏
  • 举报
回复
楼主的意思是用动态语句?不明白
kuailexq2000 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 playwarcraft 的回复:]
得,我看还是直接点
贴出表结构,给点数据
要出什么结果。。。
[/Quote]
不方便,整理起来太麻烦
kate_sun 2010-01-21
  • 打赏
  • 举报
回复
頂樓上的,有表有結果,其他都好做。
playwarcraft 2010-01-21
  • 打赏
  • 举报
回复
得,我看还是直接点
贴出表结构,给点数据
要出什么结果。。。
kuailexq2000 2010-01-21
  • 打赏
  • 举报
回复
to all:
描述的不清楚见谅了。
kuailexq2000 2010-01-21
  • 打赏
  • 举报
回复
to OrchidCat:
如上写法就是这个思路了,但是因为,会有重新整理sql,传参数的问题。会很麻烦。希望有其他避免的方法。
kuailexq2000 2010-01-21
  • 打赏
  • 举报
回复

select 2008人数统计,2009人数统计,合计人数 from(
select count(distinct 员工id) 2008人数统计 from 2008员工信息
left join
select count(distinct 员工id) 2009人数统计 from 2009员工信息
left join
select count(distinct 员工id) 合计人数 from (
select distinct 员工id 2008人数统计 from 2008员工信息
union
select distinct 员工id 2009人数统计 from 2009员工信息))


放到sql中看的清楚些。

2008员工信息是简单的描述,实际上是200多行, case 这个不现实。需要变通一下。

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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