我有如下所示的数据集。由此,我想从PersonIDs状态已更改为与上一个状态不同的每个组中选择第一行。
例如,从这个数据集中,我想要行1,4,7和11。 对此有何帮助? 如果我做GROUPBY,它只是将所有New和所有Pending分成两组。我只有SQL server 2008,所以滞后功能不起作用。
PersonID Status WhenChanged
101 New 27/01/2017 15:27
101 New 27/01/2017 16:40
101 New 27/01/2017 16:40
101 Pending 27/01/2017 16:40
101 Pending 27/01/2017 16:40
101 Pending 27/01/2017 16:40
101 New 31/01/2017 09:14
101 New 31/01/2017 10:02
101 New 31/01/2017 10:03
101 New 31/01/2017 10:05
101 Pending 03/02/2017 14:29
101 Pending 03/02/2017 14:29
答案 0 :(得分:4)
您可以使用row_numbers()
和另一个row_number()
:
select PersonID, Status, WhenChanged
from (select t.*,
row_number() over (partition by personid, status, seqnum_p - seqnum_ps
order by whenchanged
) as seqnum
from (select t.*,
row_number() over (partition by personid order by whenchanged) as seqnum_p,
row_number() over (partition by personid, status order by whenchanged) as seqnum_ps
from t
) t
) t
where seqnum = 1;
要了解其工作原理,我建议您运行子查询并查看结果。这些行号之间的差异定义了组。
另一种方法是使用outer apply
。但是,上述情况可能会有更好的表现。