Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions problem1-1194-tournament-winners.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
with player_id_score as(
select
first_player as player,
first_score as score
from
Matches

union all

select
second_player as player,
second_score as score
from
Matches )

, total_score_player as (
select
player as player_id,
sum(score) as total_score
from player_id_score
group by player
order by player
)

, player_with_rnk as (
select
players.player_id,
players.group_id,
ROW_number() over (partition by group_id order by total_score desc,player_id) as rnk
from
total_score_player tsp
inner join
players
on
tsp.player_id = players.player_id
)

select group_id,player_id
from
player_with_rnk
where rnk = 1
37 changes: 37 additions & 0 deletions problem2-1159-market-analysis2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
with user_item_sell_row_num as
(
select
seller_id,
item_id,
ROW_NUMBER() over (partition by seller_id order by order_date) as rnk
from
Orders
)

, item_name_rnk as(

select
uswrnk.item_id,
uswrnk.seller_id,
Items.item_brand
from
user_item_sell_row_num uswrnk
inner join
Items
on uswrnk.item_id = Items.item_id
where uswrnk.rnk = 2

)

select
Users.user_id as seller_id,
case
when item_name_rnk.item_brand = Users.favorite_brand then 'yes'
when item_name_rnk.item_brand != Users.favorite_brand then 'no'
when item_name_rnk.item_brand is null then 'no'
End AS 2nd_item_fav_brand
from
Users
left join
item_name_rnk
on Users.user_id = item_name_rnk.seller_id