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
13 changes: 13 additions & 0 deletions Market Analysis II.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
WITH CTE AS (
SELECT u.user_id AS 'seller_id', u.favorite_brand, o.order_date, i.item_brand, RANK() OVER(PARTITION BY u.user_id ORDER BY o.order_date) AS 'rnk' FROM Users u JOIN Orders o ON u.user_id = o.seller_id JOIN Items i ON o.item_id = i.item_id
),
ACTE AS (
SELECT c.seller_id, c.favorite_brand, c.item_brand AS 'second_item_brand' FROM CTE c WHERE c.rnk = 2
)
SELECT u.user_id AS 'seller_id', (
CASE
WHEN a.seller_id IS NULL THEN 'no'
WHEN a.second_item_brand = a.favorite_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
13 changes: 13 additions & 0 deletions Tournament Winners.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
WITH CTE 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
),
ACTE AS(
SELECT p.player_id, p.group_id, SUM(c.score) AS 'total_score' FROM Players p JOIN CTE c ON p.player_id = c.player GROUP BY p.player_id, p.group_id
),
ANCTE AS (
SELECT group_id, player_id, ROW_NUMBER() OVER (PARTITION BY group_id ORDER BY total_score DESC, player_id ASC) AS 'group_rank' FROM ACTE
)

SELECT group_id, player_id FROM ANCTE WHERE group_rank = 1