diff --git a/Consecutive Numbers.sql b/Consecutive Numbers.sql new file mode 100644 index 0000000..ca32ddc --- /dev/null +++ b/Consecutive Numbers.sql @@ -0,0 +1,8 @@ +SELECT DISTINCT l3.num AS 'ConsecutiveNums' FROM Logs l1, Logs l2, Logs l3 WHERE +l1.id = l2.id - 1 +AND +l2.id = l3.id - 1 +AND +l1.num = l2.num +AND +l2.num = l3.num; diff --git a/Dynamic Pivoting of a Table.sql b/Dynamic Pivoting of a Table.sql new file mode 100644 index 0000000..6d332d8 --- /dev/null +++ b/Dynamic Pivoting of a Table.sql @@ -0,0 +1,18 @@ +CREATE PROCEDURE PivotProducts() +BEGIN + # I was found this problem extremely challenging and was unable to find a video of this problem on the S30. + + SET group_concat_max_len = 5000; + SELECT GROUP_CONCAT(DISTINCT 'MAX(CASE WHEN store = \'', + store, + '\' THEN price ELSE NULL END) AS ', + store + ORDER BY store) INTO @sql + FROM Products; + SET @sql = CONCAT('SELECT product_id, ', + @sql, + ' FROM Products GROUP BY product_id'); + PREPARE stmt FROM @sql; + EXECUTE stmt; + DEALLOCATE PREPARE stmt; +END \ No newline at end of file diff --git a/Number of Passengers in Each Bus.sql b/Number of Passengers in Each Bus.sql new file mode 100644 index 0000000..8dad00b --- /dev/null +++ b/Number of Passengers in Each Bus.sql @@ -0,0 +1,5 @@ +WITH CTE AS( + SELECT p.passenger_id, p.arrival_time, MIN(b.arrival_time) AS 'btime' FROM Passengers p INNER JOIN Buses b ON p.arrival_time <= b.arrival_time GROUP BY p.passenger_id +) +# SELECT * FROM CTE +SELECT b.bus_id, COUNT(c.btime) AS 'passengers_cnt' FROM Buses b LEFT JOIN CTE c ON b.arrival_time = c.btime GROUP BY b.bus_id ORDER BY b.bus_id \ No newline at end of file diff --git a/User Activity.sql b/User Activity.sql new file mode 100644 index 0000000..219ff10 --- /dev/null +++ b/User Activity.sql @@ -0,0 +1 @@ +SELECT activity_date AS 'day', COUNT(DISTINCT user_id) AS 'active_users' FROM Activity WHERE activity_date > "2019-06-27" AND activity_date <= "2019-07-27" GROUP BY activity_date \ No newline at end of file