在sqlserver中的情况

时间:2017-03-31 14:32:59

标签: sql-server case-when

我尝试使用案例为我的条件显示哪个晚了,但在我使用案例后我的结果显示太多结果。

select
        grp.checktime,
        --min(CONVERT(smalldatetime,l.checktime)) as clockin,
        --max(CONVERT(smalldatetime,l.checktime)) as clockout,
        ClockIn = case when min(cast(l.checktime as time)) <= cast(sc.StartTime as time)
                        then convert(varchar(100), cast(l.checktime as time), 100)
                        else 'Late ClockIn ' + convert(varchar(100), cast(l.checktime as time), 100)
                        end,
        Clockout = case when max(cast(l.checktime as time)) >= cast(sc.EndTime as time)
                        then convert(varchar(100), cast(l.checktime as time), 100)
                        else 'Early ClockOut ' + convert(varchar(100), cast(l.checktime as time), 100)
                        end,
        l.userid,
        u.showname,
        u.BADGENUMBER
    from checkinout l
    inner join userinfo u on l.userid = u.userid
    inner join UserUsedsclasses uuc on u.userid = uuc.userid
    inner join SchClass sc on uuc.SchId = sc.schClassid
    inner join (
                    select distinct CONVERT(Date,checktime) as checktime
                    from checkinout
                    group by CONVERT(Date,checktime)
                ) as grp on grp.checktime = CONVERT(Date, l.checktime)
    where uuc.SchId = 1 and u.badgenumber = 107
    and u.badgenumber not in (79, 103, 78)
    and l.checktime >= dateadd(month, datediff(month, 0, GETDATE() )  , 0)
    and l.checktime <  dateadd(month, datediff(month, 0, GETDATE ())+1, 0)
    group by grp.checktime,l.userid,u.showname,u.BADGENUMBER,sc.StartTime,l.checktime,sc.EndTime

我的结果告诉我 enter image description here

1 个答案:

答案 0 :(得分:0)

group by更改为date的{​​{1}}组。

l.checktime