How to get the first and the last record per group in SQL Server 2008?

前端 未结 4 2027
梦毁少年i
梦毁少年i 2020-12-19 03:13

As the title suggests, I\'d like to select the first and last row of each set of rows grouped with a GROUP BY.

I\'ve this table with the following data

4条回答
  •  一整个雨季
    2020-12-19 03:21

    How to two query 'UNION'

    SELECT TOP 1 EmployeeId, AttendenceId, Intime
    FROM EmployeeAttendence
    WHERE AttendenceDate >='1/18/2020 00:00:00' 
      AND AttendenceDate <='1/18/2020 23:59:59'
    GROUP BY EmployeeId,AttendenceId,Intime
    ORDER BY AttendenceId  
    
    SELECT TOP 1 EmployeeId, AttendenceId, OutTime
    FROM EmployeeAttendence
    WHERE AttendenceDate >='1/18/2020 00:00:00' 
      AND AttendenceDate <='1/18/2020 23:59:59'
    GROUP BY EmployeeId, AttendenceId, OutTime
    ORDER BY AttendenceId desc 
    

提交回复
热议问题