diff --git a/Sql6.txt b/Sql6.txt new file mode 100644 index 0000000..22c68c0 --- /dev/null +++ b/Sql6.txt @@ -0,0 +1,31 @@ +Sql6 +#1 Problem 1 : Game Play Analysis II (https://leetcode.com/problems/game-play-analysis-ii/) + +WITH CTE AS +( +select player_id, device_id, ROW_NUMBER() OVER (partition by player_id order by event_date) as 'rnk' from Activity +) +select player_id, device_id from cte where rnk =1; + +#2 Problem 2 : Game Play Analysis III (https://leetcode.com/problems/game-play-analysis-iii/) + +select player_id, event_date, SUM(games_played) OVER (partition by player_id order by event_date) as 'games_played_so_far' from Activity; + + +#3 Problem 3 : Shortest Distance in a Plane (https://leetcode.com/problems/shortest-distance-in-a-plane/) + +select ROUND(MIN(SQRT(POW(p1.x - p2.x, 2) + POW(p1.y - p2.y, 2))),2) as 'shortest' from Point2D p1 inner join Point2D p2 on (p1.x, p1.y) < (p2.x, p2.y); + + +#4 Problem 4 : Combine Two Tables (https://leetcode.com/problems/combine-two-tables/) + +select p.firstName, p.lastName, a.city, a.state from Person p left join Address a on p.personId =a.personId; + +#5 Problem 5 : Customers with Strictly Increasing Purchases (https://leetcode.com/problems/customers-with-strictly-increasing-purchases/) + +WITH CTE AS +( + select customer_id, year(order_date) as 'year', SUM(price) as 'Price' from Orders group by year, customer_id order by customer_id, year +) + +select c1.customer_id from CTE c1 LEFT JOIN CTE c2 on c1.customer_id = c2.customer_id AND c1.year+1 = c2.year AND c1.price < c2.price group by c1.customer_id HAVING count(*)-count(c2.customer_id) =1; \ No newline at end of file