使用不连续的组分区拉出第一个和最后一个值

时间:2018-04-25 20:46:31

标签: sql sql-server

我使用的是SQL Server 2012,并且有类似的内容:

    Employee  Position          Month
    ------------------------------
    John      A                 1
    John      A                 2
    John      A                 3
    John      A                 4
    John      A                 5
    John      A                 6
    John      B                 7
    John      B                 8
    John      B                 9
    John      B                 10
    John      B                 11
    John      B                 12
    John      B                 13
    John      C                 14
    John      C                 15
    John      C                 16
    John      C                 17
    John      C                 18
    John      C                 19
    John      C                 20
    John      A                 21
    John      A                 22
    John      A                 23
    John      A                 24
    John      A                 25
    John      A                 26

我需要相同的表格,但是在开始和结束月份还有两个额外的列,如下所示:

Employee  Position          Month   Begins  Ends
--------------------------------------------
John      A                 1       1       6
John      A                 2       1       6
John      A                 3       1       6
John      A                 4       1       6
John      A                 5       1       6
John      A                 6       1       6
John      B                 7       7       13
John      B                 8       7       13
John      B                 9       7       13
John      B                 10      7       13
John      B                 11      7       13
John      B                 12      7       13
John      B                 13      7       13
John      C                 14      14      20
John      C                 15      14      20
John      C                 16      14      20
John      C                 17      14      20
John      C                 18      14      20
John      C                 19      14      20
John      C                 20      14      20
John      A                 21      21      26
John      A                 22      21      26
John      A                 23      21      26
John      A                 24      21      26
John      A                 25      21      26
John      A                 26      21      26

我尝试过这样的事情:

SELECT
    [Employee]
   ,[Position]
   ,[Month]
   ,MIN([Month]) OVER (PARTITION BY [Employee], [Position]) AS 'Begins'
   ,MAX([MONTH]) OVER (PARTITION BY [Employee], [Position]) AS 'Ends'
FROM
   tab
ORDER BY
   [Month]

但是如果是这样的话,我们就无法说出第一组"位置A" (1到6)和第二组(21到26),结果显示Position =" A"与"开始= 1"和"结束= 26",这是不可取的。

我试图在不使用递归CTE的情况下尝试这样做,因为实际查询非常长并且所使用的表格非常大,所以我主要是为了避免这种情况,但我并没有这样做。 ; t知道这是否可能

1 个答案:

答案 0 :(得分:0)

使用行号方法的差异将具有相同员工和职位的连续行分类到一个组中。

select t.*,
row_number() over(partition by employee order by [month]) 
- row_number() over (partition by employee, position order by [month]) as grp
from tbl t

然后获取每个grp的最大值和最小值。

select employee,position,month,
min(month) over(partition by employee,position,grp) as begins,
max(month) over(partition by employee,position,grp) as ends
from (select t.*,
      row_number() over(partition by employee order by [month]) 
      - row_number() over (partition by employee, position order by [month]) as grp
      from tbl t
     ) t