SQL求助

afoon 2003-12-12 02:20:08
现在有四个表:名单(UID,姓名,单位),请假人员(UID,日期),迟到人员(UID,日期),缺勤人员(UID,日期)
要将后三个表中有记录的人员按月份理出,每一条记录为:姓名,单位,请假天数,迟到天数,缺勤天数。我使用:
select 名单.姓名,名单.单位,count(请假人员.UID)from 请假人员,名单 where 请假人员.UID = 名单.UID and datepart(month,请假人员.日期) = '12' group by 名单.姓名,名单.单位
union
select 名单.姓名,名单.单位,count(迟到人员.UID)from 迟到人员,名单 where 迟到人员.UID = 名单.UID and datepart(month,迟到人员.日期) = '12' group by 名单.姓名,名单.单位
union
select 名单.姓名,名单.单位,count(缺勤人员.UID)from 缺勤人员,名单 where 缺勤人员.UID = 名单.UID and datepart(month,缺勤人员.日期) = '12' group by 名单.姓名,名单.单位
结果不正确,因为每个人不会在后三个表中都有数据,当count()为0时不加入到记录中,每一条记录都不完整,怎样保留count为0的记录?
...全文
29 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
dlpseeyou 2003-12-12
  • 打赏
  • 举报
回复
完善一下:
select a.姓名,a.单位,请假天数=isnull((select sum(1) from 请假人员 where uid=a.uid and datepart(month,请假人员.日期) = '10'),0),
迟到天数=isnull((select sum(1) from 迟到人员 where uid=a.uid and datepart(month,迟到人员.日期) = '10'),0),
缺勤天数=isnull((select sum(1) from 缺勤人员 where uid=a.uid and datepart(month,缺勤人员.日期) = '10'),0)
from 名单 a
dlpseeyou 2003-12-12
  • 打赏
  • 举报
回复

select a.姓名,a.单位,请假天数=(select sum(1) from 请假人员 where uid=a.uid and datepart(month,请假人员.日期) = '12' ),
迟到天数=(select sum(1) from 迟到人员 where uid=a.uid and datepart(month,迟到人员.日期) = '12' ),
缺勤天数=(select sum(1) from 缺勤人员 where uid=a.uid and datepart(month,缺勤人员.日期) = '12')
from 名单 a
afoon 2003-12-12
  • 打赏
  • 举报
回复
再次感谢zjcxc,呵呵,强
zjcxc 元老 2003-12-12
  • 打赏
  • 举报
回复
select a.姓名,a.单位,b.请假天数,b.迟到天数,b.缺勤天数
from 名单 a join
(
select uid=isnull(a.uid,b.uid)
,请假天数=isnull(a.请假天数,0)
,迟到天数=isnull(a.迟到天数,0)
,缺勤天数=isnull(b.缺勤天数,0)
from(
select uid=isnull(a.uid,b.uid)
,请假天数=isnull(a.请假天数,0)
,迟到天数=isnull(b.迟到天数,0)
from(
select uid,请假天数=count(*) from 请假人员
where datepart(month,日期)='12' group by uid
) a full join(
select uid,迟到天数=count(*) from 迟到人员
where datepart(month,日期)='12' group by uid
) b on a.uid=c.uid
) a full join(
select uid,缺勤天数=count(*) from 缺勤人员
where datepart(month,日期)='12' group by uid
) b on a.uid=d.uid
) b on a.uid=b.uid
afoon 2003-12-12
  • 打赏
  • 举报
回复
我不需要后三个表中没有记录的人员记录,这样的话将名单表中全部人员都列出来了
zjcxc 元老 2003-12-12
  • 打赏
  • 举报
回复
select a.姓名,a.单位
,请假天数=isnull(b.请假天数,0)
,迟到天数=isnull(c.迟到天数,0)
,缺勤天数=isnull(c.缺勤天数,0)
from 名单 a left join(
select uid,请假天数=count(*) from 请假人员
where datepart(month,请假人员.日期) = '12'
group by uid
) b on a.uid=b.uid
left join(
select uid,迟到天数=count(*) from 迟到人员
where datepart(month,请假人员.日期) = '12'
group by uid
) c on a.uid=c.uid
left join(
select uid,缺勤天数=count(*) from 缺勤人员
where datepart(month,请假人员.日期) = '12'
group by uid
) d on a.uid=d.uid
afoon 2003-12-12
  • 打赏
  • 举报
回复
还是不行啊,把后三个表没有记录的都列出来了:(
victorycyz 2003-12-12
  • 打赏
  • 举报
回复
看上面显示出来的右括号,可能打成中文标点了,注意paste后改正。
victorycyz 2003-12-12
  • 打赏
  • 举报
回复
select
a.姓名,
a.单位,
b.请假天数
c.迟到天数
d.缺勤天数
from 名单 a left join
( select uid,count(*) as 请假天数
from 请假人员
group by uid
where convert(varchar(6),日期,112)='200312' --注意也许有跨年份的记录。
)b on a.uid=b.uid
left join
( select uid,count(*) as 迟到天数
from 迟到人员
group by uid
where convert(varchar(6),日期,112)='200312'
)c on a.uid=c.uid
left join
( select uid,count(*) as 缺勤天数
from 缺勤人员
group by uid
where convert(varchar(6),日期,112)='200312'
)d on a.uid=d.uid
shuiniu 2003-12-12
  • 打赏
  • 举报
回复
select 名单.姓名,名单.单位,count(*) from 请假人员 right join 名单 on 请假人员.UID = 名单.UID and datepart(month,请假人员.日期) = '12'
group by 名单.姓名,名单.单位
union all
select 名单.姓名,名单.单位,count(*) from 迟到人员 right join 名单 on 迟到人员.UID = 名单.UID and datepart(month,迟到人员.日期) = '12' group by 名单.姓名,名单.单位
union all
select 名单.姓名,名单.单位,count(*) from 缺勤人员 right join 名单 on 缺勤人员.UID = 名单.UID and datepart(month,缺勤人员.日期) = '12' group by 名单.姓名,名单.单位
shuiniu 2003-12-12
  • 打赏
  • 举报
回复
select 名单.姓名,名单.单位,count(请假人员.UID)from 请假人员 right join 名单 on 请假人员.UID = 名单.UID and datepart(month,请假人员.日期) = '12'
group by 名单.姓名,名单.单位
union all
select 名单.姓名,名单.单位,count(迟到人员.UID)from 迟到人员 right join 名单 on 迟到人员.UID = 名单.UID and datepart(month,迟到人员.日期) = '12' group by 名单.姓名,名单.单位
union all
select 名单.姓名,名单.单位,count(缺勤人员.UID)from 缺勤人员 right join 名单 on 缺勤人员.UID = 名单.UID and datepart(month,缺勤人员.日期) = '12' group by 名单.姓名,名单.单位

afoon 2003-12-12
  • 打赏
  • 举报
回复
union all 没有用啊
shuiniu 2003-12-12
  • 打赏
  • 举报
回复
select 名单.姓名,名单.单位,count(请假人员.UID)from 请假人员,名单 where 请假人员.UID = 名单.UID and datepart(month,请假人员.日期) = '12' group by 名单.姓名,名单.单位
union all
select 名单.姓名,名单.单位,count(迟到人员.UID)from 迟到人员,名单 where 迟到人员.UID = 名单.UID and datepart(month,迟到人员.日期) = '12' group by 名单.姓名,名单.单位
union all
select 名单.姓名,名单.单位,count(缺勤人员.UID)from 缺勤人员,名单 where 缺勤人员.UID = 名单.UID and datepart(month,缺勤人员.日期) = '12' group by 名单.姓名,名单.单位
------------
???

34,588

社区成员

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

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