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

How to rank subsequent data with gaps

我有这个 table,我想对 plan_number 列进行排名和更新。 使用 DENSE_RANK 我设法对 most 数据进行了分组,但有些帐户的金额在差距前后相同,它们将被分组在一起。

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')

我希望这个 table 的排名是这样的:

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_amounts。 并按从低到高 plan_sequence.

的顺序从最旧到最新 os possible

我尝试了一个简单的 DENS_RANK,它适用于 most 的数据。 但是当数据有上限,且上限前后的数据相同时,就会被归为一组。

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

这是 select * 来自 dbo.PlanItem order by plan_account

的输出
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'

这是一种间隙和孤岛问题。

为此,使用 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<>fiddle。请注意,这使用 SQL 服务器,这与您的语法一致,但与标签不一致。