Вернуть только самое низкое значение после RANK () - PullRequest
0 голосов
/ 24 июня 2018

Я создал определенные «дела», используя:

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

Теперь мне нужно создать ранги в каждом case_id, где имя может появиться только один раз, а низшему рангу следует присвоить самое низкое some_value.

Вот пример данных:

data

И желаемый вывод:

enter image description here

1 Ответ

0 голосов
/ 25 июня 2018
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
...