mysql竖表转横表的一种思路

select '本月' as months,sum(case when date=1 and Status=1 then 1 when date=1 and Status=2 then 2 when date=1 and Status=3 then 3 else 0 end) as day1,
sum(case when date=2 and Status=1 then 1 when date=2 and Status=2 then 2 when date=2 and Status=3 then 3 else 0 end) as day2,
sum(case when date=3 and Status=1 then 1 when date=3 and Status=2 then 2 when date=3 and Status=3 then 3 else 0 end) as day3,
sum(case when date=4 and Status=1 then 1 when date=4 and Status=2 then 2 when date=4 and Status=3 then 3 else 0 end) as day4,
sum(case when date=5 and Status=1 then 1 when date=5 and Status=2 then 2 when date=5 and Status=3 then 3 else 0 end) as day5,
sum(case when date=6 and Status=1 then 1 when date=6 and Status=2 then 2 when date=6 and Status=3 then 3 else 0 end) as day6,
sum(case when date=7 and Status=1 then 1 when date=7 and Status=2 then 2 when date=7 and Status=3 then 3 else 0 end) as day7,
sum(case when date=8 and Status=1 then 1 when date=8 and Status=2 then 2 when date=8 and Status=3 then 3 else 0 end) as day8,
sum(case when date=9 and Status=1 then 1 when date=9 and Status=2 then 2 when date=9 and Status=3 then 3 else 0 end) as day9,
sum(case when date=10 and Status=1 then 1 when date=10 and Status=2 then 2 when date=10 and Status=3 then 3 else 0 end) as day10,
sum(case when date=11 and Status=1 then 1 when date=11 and Status=2 then 2 when date=11 and Status=3 then 3 else 0 end) as day11,
sum(case when date=12 and Status=1 then 1 when date=12 and Status=2 then 2 when date=12 and Status=3 then 3 else 0 end) as day12,
sum(case when date=13 and Status=1 then 1 when date=13 and Status=2 then 2 when date=13 and Status=3 then 3 else 0 end) as day13,
sum(case when date=14 and Status=1 then 1 when date=14 and Status=2 then 2 when date=14 and Status=3 then 3 else 0 end) as day14,
sum(case when date=15 and Status=1 then 1 when date=15 and Status=2 then 2 when date=15 and Status=3 then 3 else 0 end) as day15,
sum(case when date=16 and Status=1 then 1 when date=16 and Status=2 then 2 when date=16 and Status=3 then 3 else 0 end) as day16,
sum(case when date=17 and Status=1 then 1 when date=17 and Status=2 then 2 when date=17 and Status=3 then 3 else 0 end) as day17,
sum(case when date=18 and Status=1 then 1 when date=18 and Status=2 then 2 when date=18 and Status=3 then 3 else 0 end) as day18,
sum(case when date=19 and Status=1 then 1 when date=19 and Status=2 then 2 when date=19 and Status=3 then 3 else 0 end) as day19,
sum(case when date=20 and Status=1 then 1 when date=20 and Status=2 then 2 when date=20 and Status=3 then 3 else 0 end) as day20,
sum(case when date=21 and Status=1 then 1 when date=21 and Status=2 then 2 when date=21 and Status=3 then 3 else 0 end) as day21,
sum(case when date=22 and Status=1 then 1 when date=22 and Status=2 then 2 when date=22 and Status=3 then 3 else 0 end) as day22,
sum(case when date=23 and Status=1 then 1 when date=23 and Status=2 then 2 when date=23 and Status=3 then 3 else 0 end) as day23,
sum(case when date=24 and Status=1 then 1 when date=24 and Status=2 then 2 when date=24 and Status=3 then 3 else 0 end) as day24,
sum(case when date=25 and Status=1 then 1 when date=25 and Status=2 then 2 when date=25 and Status=3 then 3 else 0 end) as day25,
sum(case when date=26 and Status=1 then 1 when date=26 and Status=2 then 2 when date=26 and Status=3 then 3 else 0 end) as day26,
sum(case when date=27 and Status=1 then 1 when date=27 and Status=2 then 2 when date=27 and Status=3 then 3 else 0 end) as day27,
sum(case when date=28 and Status=1 then 1 when date=28 and Status=2 then 2 when date=28 and Status=3 then 3 else 0 end) as day28,
sum(case when date=29 and Status=1 then 1 when date=29 and Status=2 then 2 when date=29 and Status=3 then 3 else 0 end) as day29,
sum(case when date=30 and Status=1 then 1 when date=30 and Status=2 then 2 when date=30 and Status=3 then 3 else 0 end) as day30,
sum(case when date=31 and Status=1 then 1 when date=31 and Status=2 then 2 when date=31 and Status=3 then 3 else 0 end) as day31,
sum(case when Status=1 then 1 else 0 end) as attendce,
sum(case when Status=2 then 1 else 0 end) as absence,
sum(case when Status=3 then 1 else 0 end) as waitwork
from(
select date,Status from (  -- 出勤日期数据及标记状态
select round(date_format(s.recorddate,'%d')) as date,s.Status
    from Personnel l
        inner join PersonnelSignIn s on l.Id=s.PersonnelId
            where l.Id='08d735e4-ded0-e2c0-4200-9b5170000001' and date_format(recorddate,'%y%m')=date_format(now(),'%y%m')
          ORDER BY date_format(s.recorddate,'%d')
)a)b limit 1
)c

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值