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

前端 未结 4 2025
梦毁少年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:19
    select tt.id, tt.groups, tt.val, x.sstart, tt.[end] from test_table tt join
    (Select groups,First_value(start) over (partition by groups order by groups) sstart from test_table  
    Union
    Select groups,Last_Value(start) over (partition by groups order by groups) sstart from test_table) as x
    on tt.start=x.sstart
    Order by tt.groups, sstart Desc
    
    0 讨论(0)
  • 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 
    
    0 讨论(0)
  • 2020-12-19 03:31

    This is one way -

    select  t.*
    from    tbl t
    join    (
            select [group],
                   min(val) as val_1,
                   max(val) as val_2
            from   tbl
            group by [group]
            ) v
         on t.[group] = v.[group]
        and (t.val = v.val_1
         or t.val = v.val_2);
    

    Fiddle: http://sqlfiddle.com/#!3/c682f/1/0

    Another approach:

    select id, [group], val, [start], [end]
    from(
    select t.*,
           max(val) over(partition by [group]) as max_grp,
           min(val) over(partition by [group]) as min_grp
    from tbl t
    ) x
    where val in (max_grp,min_grp)
    
    0 讨论(0)
  • 2020-12-19 03:42

    How about using ROW_NUMBER:

    SQL Fiddle

    WITH Cte AS(
        SELECT *,
            RnAsc = ROW_NUMBER() OVER(PARTITION BY [group] ORDER BY val),
            RnDesc = ROW_NUMBER() OVER(PARTITION BY [group] ORDER BY val DESC)
        FROM tbl
    )
    SELECT
        id, [group], val, start, [end]
    FROM Cte
    WHERE
        RnAsc = 1 OR RnDesc = 1
    ORDER BY [group], val
    
    0 讨论(0)
提交回复
热议问题