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 new file mode 100644 index 0000000..7392da4 --- /dev/null +++ b/Problem1ConsecutiveNumebrs.sql @@ -0,0 +1,22 @@ +# Write your MySQL query statement below +-- 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 +) + +-- 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/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 diff --git a/Problem3User Activity.sql b/Problem3User Activity.sql new file mode 100644 index 0000000..5e0e93e --- /dev/null +++ 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