Skip to content

Instantly share code, notes, and snippets.

@mshakhomirov
Created November 24, 2022 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mshakhomirov/4c90a6fc8516d8264e172676a83a1048 to your computer and use it in GitHub Desktop.
Save mshakhomirov/4c90a6fc8516d8264e172676a83a1048 to your computer and use it in GitHub Desktop.
with products as (
select
2 as product_id
, 'premium_account' as product_type
, 100 as total_cost
union all
select
1 as product_id
, 'premium_group' as product_type
, 200 as total_cost
union all
select
111 as product_id
, 'bots' as product_type
, 300 as total_cost
union all
select
112 as product_id
, 'bots' as product_type
, 400 as total_cost
union all
select
113 as product_id
, 'bots' as product_type
, 500 as total_cost
union all
select
213 as product_id
, 'bots' as product_type
, 300 as total_cost
)
select * from (
select
product_id
, product_type
, total_cost as product_price
, dense_rank () over (
partition by product_type
order by total_cost desc
) price_rank
from
products
) t
where price_rank < 3
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment