Get corrosponding record Of Row in Another Column of SQL Table

前端 未结 3 1501
傲寒
傲寒 2021-01-29 06:20

I am having SQL table where the records of the employee on daily basis are stored/saved I would like to get that result in tabular format

Consider a Table As Shown Below

3条回答
  •  遇见更好的自我
    2021-01-29 06:53

    I had used a CTE which is worked for me and is as follows

    WITH cte 
    AS (SELECT Row_number() 
    OVER( 
    partition BY [name] 
    ORDER BY [name]) AS rn, *FROM   table1)           
    SELECT a.[dateandtime]  AS [IN], 
    b.[dateandtime]  AS [OUT], 
    a.[action]  AS [myaction], 
       c.[dateandtime]  AS [lunchTIME] 
    FROM   (SELECT * 
    FROM   cte a 
    WHERE  rn = 1) a 
    JOIN (SELECT * 
    FROM   cte b 
    WHERE  rn = 2) b 
    ON a.[name] = b.[name] 
    LEFT JOIN (SELECT * 
    FROM   cte b 
    WHERE  rn = 3) c 
    ON a.[name] = c.[name]  
    

提交回复
热议问题