Created
November 24, 2022 15:23
-
-
Save mshakhomirov/4c90a6fc8516d8264e172676a83a1048 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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