2017-03-31 13 views
0

私は自分の状態が遅いが、私の場合は私の結果があまりにも多くの結果を示すときに使用した後に表示するために使用しようとします。ケースin sqlserver

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

+0

「checkinout」はどのように見えますか?私はこれがLEAD()かLAG()の良いユースケースかどうか疑問に思っています。 –

+0

checkinoutはテーブル名です –

+0

明らかにテーブル名です。私はサンプルデータのようにテーブルがどのように見えるのだろうかと思っています。クエリと部分出力だけを知ることは本当に有益ではありません。 –

答えて

0
l.checktimedateによってグループにあなたの group byを変更し

示しています。

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 
     , convert(date, l.checktime) 
     , sc.EndTime