如何对有差距的后续数据进行排名

时间:2019-03-25 13:32:51

标签: sql sql-server multiple-columns rank subsequence

我有此表要排名和更新plan_number列。 使用DENSE_RANK,我设法将大多数数据分组,但是某些帐户在缺口之前和之后的金额是相同的。

CREATE TABLE dbo.PlanItem(
    plan_account                INT
,   plan_sequence               INT
,   plan_number                 INT
,   plan_amount                 DECIMAL(16,2)                               
,   plan_due_date               DATETIME                                            
    )

INSERT INTO dbo.PlanItem VALUES(12159875,1223931,0,9334.00,'2014-07-31')
INSERT INTO dbo.PlanItem VALUES(12159875,1223932,0,160584.00,'2014-08-31')
INSERT INTO dbo.PlanItem VALUES(12159875,1223933,0,9334.00,'2014-09-30')
INSERT INTO dbo.PlanItem VALUES(12159875,1223934,0,9334.00,'2014-10-31')
INSERT INTO dbo.PlanItem VALUES(12159875,1223935,0,9334.00,'2014-11-30')
INSERT INTO dbo.PlanItem VALUES(12159875,1223936,0,9334.00,'2014-12-31')
INSERT INTO dbo.PlanItem VALUES(12159875,1223937,0,9334.00,'2015-01-31')
INSERT INTO dbo.PlanItem VALUES(12159875,1223938,0,9334.00,'2015-02-28')
INSERT INTO dbo.PlanItem VALUES(12159875,1223939,0,9334.00,'2015-03-31')

我希望此表的排名如下:

12159875,1223931,1,9334.00,'2014-07-31'
12159875,1223932,2,160584.00,'2014-08-31'
12159875,1223933,3,9334.00,'2014-09-30'
12159875,1223934,3,9334.00,'2014-10-31'
12159875,1223935,3,9334.00,'2014-11-30'
12159875,1223936,3,9334.00,'2014-12-31'
12159875,1223937,3,9334.00,'2015-01-31'
12159875,1223938,3,9334.00,'2015-02-28'
12159875,1223939,3,9334.00,'2015-03-31'

分组应基于相同的后续plan_amount。 并按从最低到最高的plan_sequence顺序排列了从最早到最新的操作系统。

我尝试了一个简单的DENS_RANK,它对于大多数数据都适用。 但是,如果数据具有上限,并且上限之前和之后的数据相同,则将它们分组在一起。

WITH CTE_TEST
AS
(
SELECT *,
    DENSE_RANK() OVER (PARTITION BY plan_account ORDER BY plan_amount) AS Dense
FROM dbo.PlanItem
)

UPDATE dbo.PlanItem 
SET plan_number = Dense
FROM CTE_TEST
JOIN dbo.PlanItem ON dbo.PlanItem.plan_sequence = CTE_TEST.plan_sequence
GO

这是dbo.planItem的plan * account中的select *的输出

12159875,1223933,1,9334.00,'2014-09-30'
12159875,1223934,1,9334.00,'2014-10-31'
12159875,1223935,1,9334.00,'2014-11-30'
12159875,1223936,1,9334.00,'2014-12-31'
12159875,1223937,1,9334.00,'2015-01-31'
12159875,1223938,1,9334.00,'2015-02-28'
12159875,1223939,1,9334.00,'2015-03-31'
12159875,1223931,1,9334.00,'2014-07-31'
12159875,1223932,2,160584.00,'2014-08-31'

1 个答案:

答案 0 :(得分:1)

这是一种空白和岛屿问题。

为此,请使用lag()在先前的值上达到峰值,然后进行条件累积和:

select pi.*,
       sum(case when prev_pa = plan_amount then 0 else 1 end) over (partition by plan_account order by plan_sequence) as your_rank
from (select pi.*, lag(plan_amount) over (partition by plan_account order by plan_sequence) as prev_pa
      from planitem pi
     ) pi;

Here是db <>小提琴。请注意,这使用的是SQL Server,它与您的语法一致,但与标记一致。