From 92cf9c3617eb9b402d6ddadaa7690af38b1fea33 Mon Sep 17 00:00:00 2001 From: Feminto Date: Thu, 29 May 2025 21:13:58 -0700 Subject: [PATCH 1/5] Adding SQL solution for problem 1 --- game_analysis_2 | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 game_analysis_2 diff --git a/game_analysis_2 b/game_analysis_2 new file mode 100644 index 0000000..d0225ac --- /dev/null +++ b/game_analysis_2 @@ -0,0 +1,28 @@ +-- Method 1 +WITH cte AS ( + SELECT player_id, + MIN(event_date) AS first_login + FROM activity + GROUP BY player_id +) +SELECT a.player_id, + a.device_id +FROM activity a +JOIN cte c +ON a.player_id = c.player_id AND a.event_date = c.first_login; + +-- Method 2 +SELECT DISTINCT player_id, + FIRST_VALUE(device_id) OVER(PARTITION BY player_id ORDER BY event_date RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) device_id +FROM activity; + +-- Method 3 +SELECT player_id, + device_id +FROM ( + SELECT player_id, + device_id, + DENSE_RANK() OVER(PARTITION BY player_id ORDER BY event_date) rnk + FROM activity +)a +WHERE rnk = 1; \ No newline at end of file From fda048eed6575647b0c9b26fc6144599bf17c213 Mon Sep 17 00:00:00 2001 From: Feminto Date: Fri, 30 May 2025 19:01:14 -0700 Subject: [PATCH 2/5] Adding solutions for Problem 2 and Problem 4 --- combine_two_tables | 0 game_play_analysis_3 | 14 ++++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 combine_two_tables create mode 100644 game_play_analysis_3 diff --git a/combine_two_tables b/combine_two_tables new file mode 100644 index 0000000..e69de29 diff --git a/game_play_analysis_3 b/game_play_analysis_3 new file mode 100644 index 0000000..ce8c7b9 --- /dev/null +++ b/game_play_analysis_3 @@ -0,0 +1,14 @@ +-- Method 1 +SELECT player_id, + event_date, + SUM(games_played) OVER(PARTITION BY player_id ORDER BY event_date) AS games_played_so_far +FROM activity; + +-- Method 2 +SELECT a1.player_id, + a1.event_date, + (SELECT SUM(a2.games_played) + FROM activity a2 + WHERE a1.player_id = a2.player_id + AND a2.event_date <= a1.event_date) AS games_played_so_far +FROm activity a1; \ No newline at end of file From 9567ca45b5bc9f408aa980f437623da09f635f04 Mon Sep 17 00:00:00 2001 From: Feminto Date: Fri, 30 May 2025 19:05:23 -0700 Subject: [PATCH 3/5] Adding solution for problem 4 --- combine_two_tables | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/combine_two_tables b/combine_two_tables index e69de29..4d04a0f 100644 --- a/combine_two_tables +++ b/combine_two_tables @@ -0,0 +1,7 @@ +SELECT p.firstname, + p.lastname, + a.city, + a.state +FROM person p +LEFT JOIN address a +ON p.personid = a.personid; \ No newline at end of file From 6ceaa0265e2e3008ddc44806426ba5eaf14865b2 Mon Sep 17 00:00:00 2001 From: Feminto Date: Mon, 2 Jun 2025 20:49:06 -0700 Subject: [PATCH 4/5] Adding solution for leetcode problem ShortestDistanceInaPlane --- ShortestDistanceInaPlane.sql | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 ShortestDistanceInaPlane.sql diff --git a/ShortestDistanceInaPlane.sql b/ShortestDistanceInaPlane.sql new file mode 100644 index 0000000..6b4f343 --- /dev/null +++ b/ShortestDistanceInaPlane.sql @@ -0,0 +1,8 @@ +-- Solution for Leetcode problem ShortestDistanceInaPlane +SELECT MIN(ROUND(SQRT(POW(p1.x - p2.x, 2) + POW(p1.y - p2.y, 2)), 2)) AS shortest +FROM point2d p1 +JOIN point2d p2 +ON p1.x < p2.x OR p1.y < p2.y +/*we can also use the following*/ +-- ON (p1.x,p1.y) < (p2.x,p2.y) +; \ No newline at end of file From c9777204736824138285715d4191313f1eea4324 Mon Sep 17 00:00:00 2001 From: Feminto Date: Sun, 8 Jun 2025 16:35:12 -0700 Subject: [PATCH 5/5] Adding SQL query for Customer With Strictly Increasing purchases --- CustomersWithStrictlyIncreasingPurchases.sql | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 CustomersWithStrictlyIncreasingPurchases.sql diff --git a/CustomersWithStrictlyIncreasingPurchases.sql b/CustomersWithStrictlyIncreasingPurchases.sql new file mode 100644 index 0000000..77c8faf --- /dev/null +++ b/CustomersWithStrictlyIncreasingPurchases.sql @@ -0,0 +1,16 @@ +WITH cte AS ( + SELECT customer_id, + YEAR(order_date) AS year, + SUM(price) AS price + FROM orders + GROUP BY 1,2 + ORDER BY 1,2 +) +SELECT DISTINCT c1.customer_id +FROM cte c1 +LEFT JOIN cte c2 +ON (c1.customer_id = c2.customer_id +AND c1.year + 1 = c2.year +AND c2.price > c1.price) +GROUP BY 1 +HAVING COUNT(c1.customer_id) - COUNT(c2.customer_id) = 1; \ No newline at end of file