首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

-100分求这样的SQL语句(不够再给)-的贴子有关问题

2012-01-19 
-------100分求这样的SQL语句(不够再给)------的贴子问题zeren表TableA结构如下:bzeren(char)szeren(c)dep

-------100分求这样的SQL语句(不够再给)------的贴子问题
zeren表TableA结构如下:

bzeren(char)     szeren(c)   depart(c)   name(c)       lb(c)           kh_jj(float)   khlb(c)
大责任一             A小责任一       depa         姓名一         非劳务工     1200                 月奖金
大责任一             A小责任一       depa         姓名一         非劳务工     4800                 月工资
大责任一             A小责任一       depb         姓名二         劳务工         600                   月工资
大责任一             A小责任二       depc         姓名三         非劳务工     1400                 月工资
大责任二             B小责任一       depe         姓名四         劳务工         200                   月工资
.....
大责任N               N小责任n         depn         姓名n             劳务工         700                 额外奖励

-----------------------------------
现在想提取数据形成以下格式的表TableB(用来做报表)
-----------------------------------

bzren           szeren           部门       总人数   非劳人数   劳人数   非劳收入   劳务工收入
大责任一     A小责任一     depa
大责任一     A小责任n       depb
..
大责任N       N小责任n       depc

------------------------------------
其中TableB的bzeren、szeren、bdepart是用要求唯一的.即用下语句可以形成这三列
select   bzeren,szeren,bdepart   from   zeren  
group   by   bzeren,szeren,bdepart  
order   by   bzeren,szeren,bdepart

但是还要对TableB的后五列对应统计数据更新TableB
请问如何写这条SQL或存储过程?


已经提出贴子了:
http://community.csdn.net/Expert/topic/5581/5581718.xml?temp=.6187555

高手的回复如下;

select   bzeren,szeren,bdepart,Count(distinct   name)   as   总人数,
isnull(sum(case   when   lleibie=N '在岗职工 '   then   1   end),0)   as   在岗职工人数,
isnull(sum(case   when   lleibie=N '聘用工 '   then   1   end),0)   as   聘用工人数,
isnull(sum(case   when   lleibie=N '劳务工 '   then   1   end),0)   as   劳人数,
isnull(sum(case   when   lleibie=N '在岗职工 '   or   lleibie=N '聘用工 '   then   kh_jj   end),0)   as   非劳收入,
isnull(sum(case   when   lleibie=N '劳务工 '   then   kh_jj   end),0)   as   劳务工收入
from   zeren  
group   by   bzeren,szeren,bdepart  
order   by   bzeren,szeren,bdepart


但是有错误呀!光用
sum(case   when   lb=N '非劳务工 '   then   1   end)   as   非劳人数  
是不对的.因为总人数是用count(distinct   name)统计的,


这样统计出来的   劳务人数+非劳务人数   !=   总人数的

是否统计劳务和非劳务人数也要distinct?


[解决办法]
select bzeren,szeren,bdepart,Count(distinct name) as 总人数,
isnull(sum(case when lleibie=N '在岗职工 ' then 1 end),0) as 在岗职工人数,
isnull(sum(case when lleibie=N '聘用工 ' then 1 end),0) as 聘用工人数,
isnull(sum(case when lleibie=N '劳务工 ' then 1 end),0) as 劳人数,
isnull(sum(case when lleibie=N '在岗职工 ' or lleibie=N '聘用工 ' then kh_jj end),0) as 非劳收入,
isnull(sum(case when lleibie=N '劳务工 ' then kh_jj end),0) as 劳务工收入
from (select distinct bzeren,szeren,bdepart,name, lb from zeren)a
group by bzeren,szeren,bdepart
order by bzeren,szeren,bdepart
[解决办法]
select bzeren,szeren,bdepart,Count(*)as 总人数,
isnull(sum(case when lleibie=N '在岗职工 ' then 1 end),0) as 在岗职工人数,
isnull(sum(case when lleibie=N '聘用工 ' then 1 end),0) as 聘用工人数,
isnull(sum(case when lleibie=N '劳务工 ' then 1 end),0) as 劳人数,
isnull(sum(case when lleibie=N '在岗职工 ' or lleibie=N '聘用工 ' then kh_jj end),0) as 非劳收入,
isnull(sum(case when lleibie=N '劳务工 ' then kh_jj end),0) as 劳务工收入
from (
select bzeren,szeren,bdepart,name,lleibie,sum(kh_jj)as kh_jj
from zeren
group by bzeren,szeren,bdepart,name,lleibie
) as t
group by bzeren,szeren,bdepart
order by bzeren,szeren,bdepart

热点排行