From 0bbad280abeea8778d17c81033737f4453a8272d Mon Sep 17 00:00:00 2001 From: SukeshSrinivas <123338848+SukeshSrinivas@users.noreply.github.com> Date: Sat, 21 Dec 2024 01:55:37 -0600 Subject: [PATCH 1/4] Problem 1 Completed --- Problem1ConsecutiveNumebrs.sql | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Problem1ConsecutiveNumebrs.sql diff --git a/Problem1ConsecutiveNumebrs.sql b/Problem1ConsecutiveNumebrs.sql new file mode 100644 index 0000000..6579b71 --- /dev/null +++ b/Problem1ConsecutiveNumebrs.sql @@ -0,0 +1,11 @@ +# Write your MySQL query statement below +WITH CTE AS(SELECT e1.id as 'ID', e1.num as 'CurrentNum', e2.num as 'NextNum', e3.num as '2ndNextNum' +FROM Logs e1 +LEFT JOIN Logs e2 +ON e1.id = e2.id-1 +LEFT JOIN Logs e3 +ON e1.id = e3.id-2 +) + +SELECT DISTINCT CurrentNum AS 'ConsecutiveNums' FROM CTE +WHERE CTE.CurrentNum = CTE.NextNum AND CTE.NextNum = CTE.2ndNextNum From b0faeebd9554aa813bd4f3e5f08f3ce9bf2df03c Mon Sep 17 00:00:00 2001 From: SukeshSrinivas <123338848+SukeshSrinivas@users.noreply.github.com> Date: Sun, 22 Dec 2024 21:35:37 -0600 Subject: [PATCH 2/4] Completed --- Problem2PassengersinEachBus.sql | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Problem2PassengersinEachBus.sql diff --git a/Problem2PassengersinEachBus.sql b/Problem2PassengersinEachBus.sql new file mode 100644 index 0000000..b921a1b --- /dev/null +++ b/Problem2PassengersinEachBus.sql @@ -0,0 +1,16 @@ +WITH BusTime AS ( + SELECT bus_id, arrival_time, LAG(arrival_time,1,0) OVER (ORDER BY arrival_time) AS previous_arrival_time +FROM Buses +), +FinalArrivalTime AS ( +SELECT b.bus_id, p.passenger_id +FROM BusTime b +LEFT JOIN Passengers p +ON p.arrival_time>b.previous_arrival_time AND p.arrival_time<=b.arrival_time +) + + +SELECT f.bus_id, COUNT(f.passenger_id) AS passengers_cnt +FROM FinalArrivalTime f +GROUP BY f.bus_id +ORDER BY f.bus_id From a6e6ff5cef1fe53c26e4fc7a19e4178a477a612a Mon Sep 17 00:00:00 2001 From: SukeshSrinivas <123338848+SukeshSrinivas@users.noreply.github.com> Date: Fri, 27 Dec 2024 11:32:40 -0600 Subject: [PATCH 3/4] Completed --- Problem3User Activity.sql | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Problem3User Activity.sql diff --git a/Problem3User Activity.sql b/Problem3User Activity.sql new file mode 100644 index 0000000..e69de29 From 270f1151ab611aaa65eb93e0474849b8437f5eb5 Mon Sep 17 00:00:00 2001 From: SukeshSrinivas <123338848+SukeshSrinivas@users.noreply.github.com> Date: Sat, 4 Jan 2025 02:33:15 -0600 Subject: [PATCH 4/4] Done --- Problem 4 Dynamic Pivoting of a Table.sql | 20 +++++++++++++++++ Problem1ConsecutiveNumebrs.sql | 27 ++++++++++++++++------- Problem3User Activity.sql | 8 +++++++ 3 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 Problem 4 Dynamic Pivoting of a Table.sql diff --git a/Problem 4 Dynamic Pivoting of a Table.sql b/Problem 4 Dynamic Pivoting of a Table.sql new file mode 100644 index 0000000..8e69d65 --- /dev/null +++ b/Problem 4 Dynamic Pivoting of a Table.sql @@ -0,0 +1,20 @@ +CREATE PROCEDURE PivotProducts() +BEGIN + + SET SESSION GROUP_CONCAT_MAX_LEN = 1000000; + # Step 1: Generate dynamic SQL for pivot columns + SELECT GROUP_CONCAT(DISTINCT CONCAT('SUM(IF(store = "', store, '", price, NULL)) AS `', store, '`')) INTO @sql + FROM products; + + # Step 2: Construct the complete dynamic SQL query + SET @sql = CONCAT( + 'SELECT product_id, ', + @sql, + ' FROM products GROUP BY product_id' + ); + # Step 3: Prepare and execute the dynamic SQL + PREPARE stmt FROM @sql; + EXECUTE stmt; + DEALLOCATE PREPARE stmt; + +END \ No newline at end of file diff --git a/Problem1ConsecutiveNumebrs.sql b/Problem1ConsecutiveNumebrs.sql index 6579b71..7392da4 100644 --- a/Problem1ConsecutiveNumebrs.sql +++ b/Problem1ConsecutiveNumebrs.sql @@ -1,11 +1,22 @@ # Write your MySQL query statement below -WITH CTE AS(SELECT e1.id as 'ID', e1.num as 'CurrentNum', e2.num as 'NextNum', e3.num as '2ndNextNum' -FROM Logs e1 -LEFT JOIN Logs e2 -ON e1.id = e2.id-1 -LEFT JOIN Logs e3 -ON e1.id = e3.id-2 +-- Step 1: Create a Common Table Expression (CTE) to compare the current row with the next two rows +WITH ConsecutiveNumsCTE AS ( + SELECT + l1.id AS CurrentID, -- ID of the current row + l1.num AS CurrentNum, -- Number of the current row + l2.num AS NextNum, -- Number from the next row + l3.num AS SecondNextNum -- Number from the second next row + FROM Logs l1 + -- Join the table with itself to get the next row (ID = CurrentID + 1) + LEFT JOIN Logs l2 + ON l1.id = l2.id - 1 + -- Join the table again to get the second next row (ID = CurrentID + 2) + LEFT JOIN Logs l3 + ON l1.id = l3.id - 2 ) -SELECT DISTINCT CurrentNum AS 'ConsecutiveNums' FROM CTE -WHERE CTE.CurrentNum = CTE.NextNum AND CTE.NextNum = CTE.2ndNextNum +-- Step 2: Select numbers that appear three times consecutively +SELECT DISTINCT CurrentNum AS ConsecutiveNums +FROM ConsecutiveNumsCTE +-- Filter rows where the current number is equal to both the next and the second next number +WHERE CurrentNum = NextNum AND NextNum = SecondNextNum; \ No newline at end of file diff --git a/Problem3User Activity.sql b/Problem3User Activity.sql index e69de29..5e0e93e 100644 --- a/Problem3User Activity.sql +++ b/Problem3User Activity.sql @@ -0,0 +1,8 @@ +WITH CTE AS ( + SELECT DISTINCT activity_date as day, user_id FROM Activity +WHERE activity_date BETWEEN DATE_ADD( '2019-07-27' , INTERVAL -29 DAY) AND '2019-07-27' +) + +SELECT CTE.day, COUNT(DISTINCT user_id) AS active_users +FROM CTE +GROUP BY CTE.day \ No newline at end of file