From 09f2c9ed4cf4c8640c13eaf6155afe2a677e8427 Mon Sep 17 00:00:00 2001 From: tejas274 Date: Sun, 8 Jun 2025 14:24:48 -0500 Subject: [PATCH] problem 1 and problem 2 added --- problem1-1194-tournament-winners.sql | 41 ++++++++++++++++++++++++++++ problem2-1159-market-analysis2.sql | 37 +++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 problem1-1194-tournament-winners.sql create mode 100644 problem2-1159-market-analysis2.sql diff --git a/problem1-1194-tournament-winners.sql b/problem1-1194-tournament-winners.sql new file mode 100644 index 0000000..8c40f4f --- /dev/null +++ b/problem1-1194-tournament-winners.sql @@ -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 \ No newline at end of file diff --git a/problem2-1159-market-analysis2.sql b/problem2-1159-market-analysis2.sql new file mode 100644 index 0000000..802751e --- /dev/null +++ b/problem2-1159-market-analysis2.sql @@ -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 \ No newline at end of file