记录解决方案(sqlserver篇)

  1. 一个月的补卡次数不超过三次(即统计一个月内某人的补卡次数)

    表结构是某人一天内的四次打卡状态,这样是统计当月补卡的天数了(错误)

    select count(*) from [Proc_HR_Punch]where Account=@account TaskState='approved' and (state='补卡' or state1='补卡' or state2='补卡' or state3='补卡') and 
    convert(varchar(7),Yeaterdaydate,120)=convert(varchar(7),@date,120)

    解决方案:把行转为列即可

    with M as(
    select Account,ISbk,statue,date from[Proc_HR_PunchException] 
    unpivot(ISbk for statue in (state,state1,state2,state3))a where a.ISbk='补卡' 
    and Account=@Account and TaskState='approved' and convert(varchar(7),date,120)=convert(varchar(7),@date,120))
    select Count(*)from M

  2. 存储过程里使用视图并  对查询结果再次过滤查询

 

posted on 2023-01-28 17:46  独行者ˇ  阅读(40)  评论(0)    收藏  举报