diff --git a/HW3.txt b/HW3.txt new file mode 100644 index 0000000..44b3841 --- /dev/null +++ b/HW3.txt @@ -0,0 +1,42 @@ +1 Problem 1 : Consecutive Numbers (https://leetcode.com/problems/consecutive-numbers/) + +# Write your MySQL query statement below +select distinct l1.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; + +2 Problem 2 :Number of Passengers in Each Bus ( https://leetcode.com/problems/the-number-of-passengers-in-each-bus-i/ ) + +with cte as +(select b.bus_id, 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 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; + + + +3 Problem 3 :User Activity (https://leetcode.com/problems/user-activity-for-the-past-30-days-i/ ) + +select activity_date as 'day', count(distinct user_id) as 'active_users' from Activity where activity_date > '2019-06-27'AND activity_date <= '2019-06-27' group by activity_date; + + + +4 Problem 4 :Dynamic Pivoting of a Table ( https://leetcode.com/problems/dynamic-pivoting-of-a-table/ ) + +SET @sql = NULL; + +SELECT GROUP_CONCAT( + DISTINCT CONCAT( + 'MAX(CASE WHEN course_id = ', course_id, + ' THEN grade END) AS `', course_id, '`' + ) + ) +INTO @sql +FROM Scores; + +SET @sql = CONCAT('SELECT student_id, ', @sql, ' FROM Scores GROUP BY student_id'); + +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + + + +