SQL查询问题,高手帮帮忙。。。

风影萧诺 2011-03-28 12:38:15
有张学生出勤表 stu_table
字段:stu_id,stu_type (注:stu_type: 1 = 出勤,2 = 病假/事假,3 = 迟到/早退 4 = 旷课)

用一条sql返回以下格式结果:

应出勤 实出勤 出勤率 病假/事假 迟到/早退 旷课


...全文
115 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
风影萧诺 2011-03-28
  • 打赏
  • 举报
回复
很专业。。。 谢谢了。。。
就是统计整个班级的。。、。。
--小F-- 2011-03-28
  • 打赏
  • 举报
回复
select
stu_id,
count(1) as 应出勤,
sum(case wtu_type when 1 then 1 else 0 end) as [实出勤],
ltrim(sum(case wtu_type when 1 then 1 else 0 end)*100.0/count(1))+'%' as 出勤率,
sum(case wtu_type when 2 then 1 else 0 end) as [病假/事假],
sum(case wtu_type when 3 then 1 else 0 end) as [迟到/早退],
sum(case wtu_type when 4 then 1 else 0 end) as [旷课]
from
tb
group by
stu_id

--如果不是统计每个学生的 就这样
select
count(1) as 应出勤,
sum(case wtu_type when 1 then 1 else 0 end) as [实出勤],
ltrim(sum(case wtu_type when 1 then 1 else 0 end)*100.0/count(1))+'%' as 出勤率,
sum(case wtu_type when 2 then 1 else 0 end) as [病假/事假],
sum(case wtu_type when 3 then 1 else 0 end) as [迟到/早退],
sum(case wtu_type when 4 then 1 else 0 end) as [旷课]
from
tb
--小F-- 2011-03-28
  • 打赏
  • 举报
回复
select
stu_id,
count(1) as 应出勤,
sum(case wtu_type when 1 then 1 else 0 end) as [实出勤],
ltrim(sum(case wtu_type when 1 then 1 else 0 end)*100.0/count(1))+'%' as 出勤率,
sum(case wtu_type when 2 then 1 else 0 end) as [病假/事假],
sum(case wtu_type when 3 then 1 else 0 end) as [迟到/早退],
sum(case wtu_type when 4 then 1 else 0 end) as [旷课]
group by
stu_id

27,582

社区成员

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

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