我有一个float
值的表格。我想使用这些值的子集(A)来使用PERCENT_RANK()
创建排名。然后,我想基于从第一子集(A)导出的等级将等级分配给表中的值的第二(非交叉)子集(B)。简单地将(B)中的值与(A)中的值相结合不会起作用,因为来自子集(B)的值通常在子集(A)中获得相等的值。在这种情况下,我可以使用最近的值"接近或线性插值"获得排名的方法。我喜欢速度和简单性,因为我处理了数十万行。
这是一个具体的例子(假设子集A是Flag = 0,子集B是Flag = 1):
DECLARE @Data TABLE
(
Value FLOAT,
Flag BIT
)
INSERT INTO @Data SELECT 0.081, 0
INSERT INTO @Data SELECT 0.831, 0
INSERT INTO @Data SELECT 0.798, 0
INSERT INTO @Data SELECT 0.722, 0
INSERT INTO @Data SELECT 0.322, 0
INSERT INTO @Data SELECT 0.186, 0
INSERT INTO @Data SELECT 0.494, 0
INSERT INTO @Data SELECT 0.757, 0
INSERT INTO @Data SELECT 0.996, 0
INSERT INTO @Data SELECT 0.146, 0
INSERT INTO @Data SELECT 0.514, 1
INSERT INTO @Data SELECT 0.787, 1
INSERT INTO @Data SELECT 0.125, 1
INSERT INTO @Data SELECT 0.324, 1
INSERT INTO @Data SELECT 0.86, 1
--Subset A
SELECT *,
Rnk = PERCENT_RANK() OVER (ORDER BY Value)
FROM @Data
WHERE Flag = 0
--Subset B
SELECT *,
Rnk = ?--Ranking based on ranks derived from subset A
FROM @Data
WHERE Flag = 1
答案 0 :(得分:1)
with a as (
select d.*
PERCENT_RANK() OVER (ORDER BY Value) as rnk
from @Data d
where Flag = 0
)
select b.*, a.rnk
from @Data b outer join
(select top 1 *
from a
where a.value <= b.value
order by a.value desc
) a
where Flag = 1;