Count summary records per month with conditional SQL

后端 未结 3 2079
无人共我
无人共我 2021-01-29 03:37

I have a table, let\'s call them table SUMMARYDATA

NIP  NAME   DEPARTMENT       STATUSIN           STATUSOUT             TOTALLOSTTIME 
------------         


        
3条回答
  •  庸人自扰
    2021-01-29 04:06

    Try the following

    select x.department
           ,x.month
           ,x.year     
           ,count(case when x.sum_lost_time >'02:00:00' then NAME end) as RTOTALLOSTTIME 
     from (select MONTH(STATUSIN)  as [month]
                   ,YEAR(STATUSIN) as [year]
                   ,NIP
                   ,NAME
                   ,DEPARTMENT         
                   ,convert(varchar,dateadd(second,sum(datediff(second,'00:00:00',cast(TOTALLOSTTIME as time))),0),108) as sum_lost_time
              from SUMMARYDATA b 
          group by MONTH(STATUSIN)
                   ,YEAR(STATUSIN)
                   ,NIP
                   ,NAME
                   ,DEPARTMENT
            )x
    group by  x.department
             ,x.month
             ,x.year        
    

提交回复
热议问题