separat data as per column base

  • i have data in colum                                  

    EmpID      DateTime   StatusINOUT
    1001        2018-05-26 09:33        1
    1001       2018-05-26 08:44        2
    1002        2018-05-28 08:22        1

    1002         2018-05-28 12:442                 
    1001        2018-05-21 07:441
    1001  2018-05-21 10:442
    1002  2018-05-23 11:111

    1002        2018-05-23 09:442              

     

    Now i want to display Status wise INTIME(1) and OUTTIME(2) it  as below

    EmpidINTIME(1)OUTIME(2)
    12018-05-26 08:33 2018-05-26 08:44
    2 2018-05-28 08:222018-05-28 12:44
    12018-05-21 07:44 2018-05-21 10:44
    22018-05-23 11:112018-05-23 09:44

     
  • SELECT *
    INTO #t
    FROM (VALUES
    (1001, CONVERT(datetime,'2018-05-26 09:33'), 1),
    (1001, '2018-05-26 08:44', 2),
    (1002, '2018-05-28 08:22', 1),
    (1002, '2018-05-28 12:44', 2),
    (1001, '2018-05-21 07:44', 1),
    (1001, '2018-05-21 10:44', 2),
    (1002, '2018-05-23 11:11', 1),
    (1002, '2018-05-23 09:44', 2)) T(EmpID, myDateTime, StatusINOUT)

    ;WITH CTE AS
    (
        SELECT EmpID,
               CASE WHEN StatusINOUT= 1 THEN myDateTime ELSE NULL END [INTIME],
               CASE WHEN StatusINOUT= 2 THEN myDateTime ELSE NULL END [OUTTIME],
               CONVERT(date,myDateTime) myDate
          FROM #t
    )
    SELECT x.EmpID,
           MAX(x.[INTIME]) [INTIME],
           MAX(x.[OUTTIME]) [OUTTIME]
      FROM CTE x
     GROUP BY EmpId, myDate
    GO
    DROP TABLE #t

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply