Filtering by RANK() in HAVING clause without subqueries

我怕爱的太早我们不能终老 提交于 2019-12-12 11:04:53

问题


I'm fetching group-wise maximums (the data CTE emulates a set of joins from actual tables):

with data as (
    select 'Austria' as country, 1 as id, 'red' as colour, 120 as quantity
    union all select 'Austria', 2, 'green', 96
    union all select 'Austria', 3, 'blue', 103

    union all select 'Belgium', 1, 'red', 33
    union all select 'Belgium', 2, 'green', 12
    union all select 'Belgium', 3, 'blue', 40
)
select country, colour, quantity
from (
    select country, colour, quantity,
    rank() over (partition by country order by quantity desc, id) as position
    from data
    group by country, id, colour, quantity
) subquery
where position=1;

This works fine but I'm forced to wrap the query with the RANK() call inside a subquery, because this alternative triggers a syntax error:

-- [...]
select country, colour, quantity,
rank() over (partition by country order by quantity desc, id) as position
from data
group by country, id, colour, quantity
having rank() over (partition by country order by quantity desc, id)=1;

Windowed functions can only appear in the SELECT or ORDER BY clauses.

Is there an alternative syntax to avoid this limitation or the subquery is the only sensible way?

The ultimate goal is to integrate this code into a larger set of dynamically generated SQL expressions. If I can keep a single query I can just define the different parts (select, joined tables, where, group by, having and order by) with arrays. Otherwise I need to think a major rewrite.


回答1:


you can use top 1 with ties as below

select top (1) with ties country, colour, quantity,
    rank() over (partition by country order by quantity desc, id) as position
from data
--group by country, id, colour, quantity
order by rank() over (partition by country order by quantity desc, id)



回答2:


If you look at performance differences i still feel subquery approach is better than the above top (1) with ties approach because of below sort operator:

Eventhough top (1) with ties is more elegant... Below are the performance differences based on Execution plan snapshot



来源:https://stackoverflow.com/questions/44310873/filtering-by-rank-in-having-clause-without-subqueries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!