diff --git a/Problem1.sql b/Problem1.sql new file mode 100644 index 0000000..d71761f --- /dev/null +++ b/Problem1.sql @@ -0,0 +1,9 @@ +# Write your MySQL query statement below +select distinct(num) as consecutiveNums +from ( + select id,num, + lag(num) over(order by id) as pervious_num, + lead(num) over(order by id) as next_num + from logs +) sq +where sq.num=sq.pervious_num and sq.num=sq.next_num; \ No newline at end of file diff --git a/Problem2.sql b/Problem2.sql new file mode 100644 index 0000000..c222e28 --- /dev/null +++ b/Problem2.sql @@ -0,0 +1,10 @@ +SELECT b.id AS bus_id, COUNT(p.passenger_id) AS passengers_cnt +FROM Buses b +LEFT JOIN ( + SELECT p.passenger_id, MIN(b.id) AS bb + FROM Passengers p + JOIN Buses b ON p.arrival_time <= b.arrival_time + GROUP BY p.passenger_id +) AS p ON b.id = p.bb +GROUP BY b.id +ORDER BY b.id; \ No newline at end of file diff --git a/Problem4.sql b/Problem4.sql new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md index a991856..ef64880 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Sql3 -1 Problem 1 : Consecutive Numbers (https://leetcode.com/problems/consecutive-numbers/) +Problem 1 : Consecutive Numbers (https://leetcode.com/problems/consecutive-numbers/) 2 Problem 2 :Number of Passengers in Each Bus ( https://leetcode.com/problems/the-number-of-passengers-in-each-bus-i/ ) diff --git a/problem3.sql b/problem3.sql new file mode 100644 index 0000000..af84519 --- /dev/null +++ b/problem3.sql @@ -0,0 +1,6 @@ +# Write your MySQL query statement below +select activity_date as day,count(DISTINCT user_id) as active_users +from Activity +where datediff('2019-07-27',activity_date) <30 +and activity_date <= '2019-07-27' +group by activity_date;