仅返回 RANK() 之后的最小值

Posted

技术标签:

【中文标题】仅返回 RANK() 之后的最小值【英文标题】:Return only the lowest value after RANK() 【发布时间】:2018-06-24 11:07:47 【问题描述】:

我使用以下方法创建了某些“案例”:

RANK() OVER(ORDER BY name, A, B, some_value) as case_id

现在我需要在每个 case_id 中创建等级,其中名称只能出现一次,并且最低等级应该分配给最低的 some_value。

这是数据示例:

以及所需的输出:

【问题讨论】:

你的意思是你想要case_id之后的最低值?示例数据和预期输出在这里真的很有帮助。 【参考方案1】:
Select * from ( 

    Select Name, A, B, some_value, case_id 
    , ROWNO() over (partition by case_id, Name order by some_value) as R1
    from (
        Select 'SF' as Name, 'y' as A, 'n' as B, 120 as some_value, 1 as case_id
        union all Select 'NY' as Name, 'y' as A, 'n' as B, 150 as some_value, 1 as case_id
        union all Select 'LA' as Name, 'y' as A, 'n' as B, 155 as some_value, 1 as case_id
        union all Select 'SF' as Name, 'y' as A, 'n' as B, 160 as some_value, 1 as case_id
        union all Select 'SF' as Name, 'n' as A, 'y' as B, 110 as some_value, 5 as case_id
        union all Select 'NY' as Name, 'n' as A, 'y' as B, 120 as some_value, 5 as case_id
        union all Select 'LA' as Name, 'n' as A, 'y' as B, 125 as some_value, 5 as case_id
        union all Select 'LA' as Name, 'n' as A, 'y' as B, 140 as some_value, 5 as case_id
        union all Select 'NY' as Name, 'n' as A, 'y' as B, 155 as some_value, 5 as case_id
    ) testdata

    ) data
Where R1 = 1
order by case_id, some_value

【讨论】:

以上是关于仅返回 RANK() 之后的最小值的主要内容,如果未能解决你的问题,请参考以下文章

python 对于每个键,仅选取最小(或最大)值作为值

旋转数组的最小值

以下算法在未排序数组中找到最小值的复杂性

Java泛型,返回数组最大值最小值

matlab一组数据里面最小值,有没有函数可以直接找出来?

返回最小值的键[重复]