From a3b44ef30dca5b48cb29f5f6bcd65a495ffa5c12 Mon Sep 17 00:00:00 2001 From: Radhika Tekade Date: Fri, 3 Jan 2025 10:00:53 -0800 Subject: [PATCH 1/2] Create market_analysis_II --- market_analysis_II.sql | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 market_analysis_II.sql diff --git a/market_analysis_II.sql b/market_analysis_II.sql new file mode 100644 index 0000000..07abd5d --- /dev/null +++ b/market_analysis_II.sql @@ -0,0 +1,16 @@ +# Write your MySQL query statement below +WITH CTE AS( + SELECT seller_id, order_date, item_id, + RANK() OVER (PARTITION BY seller_id ORDER BY order_date) AS 'rnk' + FROM Orders +), +ACTE AS( + SELECT c.seller_id, c.item_id, i.item_brand FROM CTE c LEFT JOIN Items i USING (item_id) WHERE c.rnk = 2 +) + +SELECT u.user_id AS 'seller_id', ( + CASE + WHEN u.favorite_brand = a.item_brand THEN 'yes' + ELSE 'no' + END +) AS '2nd_item_fav_brand' FROM Users u LEFT JOIN ACTE a ON u.user_id = a.seller_id \ No newline at end of file From 73f1ca7e17a332089ec8f8ded5fe363ef7bc8c1a Mon Sep 17 00:00:00 2001 From: Radhika Tekade Date: Sat, 4 Jan 2025 19:02:01 -0800 Subject: [PATCH 2/2] Create tournament_winners --- tournament_winners_sql | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 tournament_winners_sql diff --git a/tournament_winners_sql b/tournament_winners_sql new file mode 100644 index 0000000..3710c95 --- /dev/null +++ b/tournament_winners_sql @@ -0,0 +1,12 @@ +# Write your MySQL query statement below +WITH CTE AS( + SELECT m1.first_player AS 'player_id', m1.first_score AS 'score' FROM Matches m1 + UNION ALL + SELECT m2.second_player AS 'player_id', m2.second_score AS 'score' FROM Matches m2 +), +ACTE AS( + SELECT c.player_id, p.group_id, SUM(c.score) AS 'total' + FROM CTE c JOIN Players p USING(player_id) GROUP BY c.player_id) + +SELECT DISTINCT a.group_id, FIRST_VALUE(a.player_id) OVER +(PARTITION BY a.group_id ORDER BY a.total DESC, a.player_id) AS 'player_id' FROM ACTE a \ No newline at end of file