Skip to content

Instantly share code, notes, and snippets.

@mshakhomirov
Created November 24, 2022 15:22
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/459b68c5f3d1e8284c01e516db1d8dcb to your computer and use it in GitHub Desktop.
Save mshakhomirov/459b68c5f3d1e8284c01e516db1d8dcb to your computer and use it in GitHub Desktop.
with top_spenders as (
select 1 as user_id, 100 as total_spend, 11 as reputation_level union all
select 2 as user_id, 250 as total_spend, 11 as reputation_level union all
select 3 as user_id, 250 as total_spend, 11 as reputation_level union all
select 4 as user_id, 300 as total_spend, 11 as reputation_level union all
select 11 as user_id, 1000 as total_spend, 22 as reputation_level union all
select 22 as user_id, 1500 as total_spend, 22 as reputation_level union all
select 33 as user_id, 1500 as total_spend, 22 as reputation_level union all
select 44 as user_id, 2500 as total_spend, 22 as reputation_level
)
select
user_id
, rank() over(partition by reputation_level order by total_spend desc) as rank
, dense_rank() over(partition by reputation_level order by total_spend desc) as dense_rank
from
top_spenders
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment