From cab62a5adbc0dc7ef82d4e16f79db456b9994dab Mon Sep 17 00:00:00 2001 From: SAI NIHITH IMMANENI Date: Thu, 2 Jan 2025 23:51:40 -0600 Subject: [PATCH] Done PR6 --- Combine Two Tables.sql | 5 +++++ ...ers with Strictly Increasing Purchases.sql | 14 +++++++++++++ Game Play Analysis II.sql | 21 +++++++++++++++++++ Game Play Analysis III.sql | 8 +++++++ Shortest Distance in a Plane.sql | 20 ++++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 Combine Two Tables.sql create mode 100644 Customers with Strictly Increasing Purchases.sql create mode 100644 Game Play Analysis II.sql create mode 100644 Game Play Analysis III.sql create mode 100644 Shortest Distance in a Plane.sql diff --git a/Combine Two Tables.sql b/Combine Two Tables.sql new file mode 100644 index 0000000..4dbd8d4 --- /dev/null +++ b/Combine Two Tables.sql @@ -0,0 +1,5 @@ + +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 diff --git a/Customers with Strictly Increasing Purchases.sql b/Customers with Strictly Increasing Purchases.sql new file mode 100644 index 0000000..8285e73 --- /dev/null +++ b/Customers with Strictly Increasing Purchases.sql @@ -0,0 +1,14 @@ + +WITH CTE AS ( + SELECT customer_id, YEAR(order_date) AS 'year', SUM(price) AS price_m + FROM Orders GROUP BY year,customer_id + ORDER BY customer_id +) + +-- SELECT * FROM CTE + +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_m < c2.price_m) +GROUP BY c1.customer_id HAVING COUNT(*) - COUNT(c2.customer_id) =1 \ No newline at end of file diff --git a/Game Play Analysis II.sql b/Game Play Analysis II.sql new file mode 100644 index 0000000..4023d22 --- /dev/null +++ b/Game Play Analysis II.sql @@ -0,0 +1,21 @@ +# Write your MySQL query statement below +-- SELECT DISTINCT player_id, FIRST_VALUE(device_id) OVER (PARTITION BY player_id ORDER BY event_date) AS device_id FROM +-- Activity +-- method2 +-- SELECT a.player_id, a.device_id +-- FROM Activity a +-- WHERE a.event_date IN (SELECT MIN(b.event_date) FROM +-- Activity b WHERE a.player_id = b.player_id) + +WITH CTE AS( + SELECT player_id, device_id, RANK() OVER(PARTITION BY player_id ORDER BY event_date) AS 'rnk' + FROM Activity +) + +SELECT c.player_id , c.device_id FROM CTE c +WHERE c.rnk = '1' + + + + + diff --git a/Game Play Analysis III.sql b/Game Play Analysis III.sql new file mode 100644 index 0000000..b49b6c8 --- /dev/null +++ b/Game Play Analysis III.sql @@ -0,0 +1,8 @@ + +SELECT player_id, event_date, SUM(games_played) OVER(PARTITION BY player_id ORDER BY event_date) AS games_played_so_far +FROM Activity + +-- SELECT a1.player_id, a1.event_date, (SELECT SUM(a2.games_played) FROM Activity a2 WHERE +-- a1.player_id = a2.player_id AND a1.event_date >= a2.event_date) AS games_played_so_far + +-- FROM Activity a1 diff --git a/Shortest Distance in a Plane.sql b/Shortest Distance in a Plane.sql new file mode 100644 index 0000000..1bbe298 --- /dev/null +++ b/Shortest Distance in a Plane.sql @@ -0,0 +1,20 @@ + +-- WITH CTE AS ( +-- SELECT p1.x AS x1, p1.y AS y1 ,p2.x AS x2, p2.y AS y2, ROUND(SQRT( POW(p2.x-p1.x,2) + POW(p2.y-p1.y,2) ),2) AS 'dist' +-- FROM Point2D p1 +-- CROSS JOIN +-- Point2D p2 +-- ) + + +-- SELECT MIN(dist) AS shortest +-- FROM CTE +-- WHERE x1!=x2 OR y1!=y2 + +SELECT MIN(ROUND(SQRT(POW(p2.x - p1.x , 2) + POW(p2.y - p1.y, 2)),2)) AS 'shortest' +FROM point2D p1 +INNER JOIN point2D p2 +ON + (p1.x <= p2.x AND p1.y < p2.y) OR + (p1.x <= p2.x AND p1.y > p2.y) OR + (p1.x > p2.x AND p1.y = p2.y);