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
16 changes: 16 additions & 0 deletions CustomersWithStrictlyIncreasingPurchases.sql
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 8 additions & 0 deletions ShortestDistanceInaPlane.sql
Original file line number Diff line number Diff line change
@@ -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)
;
7 changes: 7 additions & 0 deletions combine_two_tables
Original file line number Diff line number Diff line change
@@ -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;
28 changes: 28 additions & 0 deletions game_analysis_2
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 14 additions & 0 deletions game_play_analysis_3
Original file line number Diff line number Diff line change
@@ -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;