Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Problem1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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
7 changes: 7 additions & 0 deletions Problem2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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 b.bus_id, COUNT(c.btime) as 'passengers_cnt' from buses b LEFT JOIN CTE c
ON c.btime = b.arrival_time
Group By b.bus_id
5 changes: 5 additions & 0 deletions Problem3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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
15 changes: 15 additions & 0 deletions Problem4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE PROCEDURE PivotProducts()
BEGIN

SET SESSION GROUP_CONCAT_MAX_LEN = 1000000;
SELECT GROUP_CONCAT(DISTINCT CONCAT('SUM(IF(store = "',store , '" , price , null)) as ' , store))
INTo @sql from products;

SET @sql = CONCAT('SELECT product_id ,' , @sql , ' from products GROUP by 1');


PREPARE statement from @sql;
EXECUTE statement;
DEALLOCATE PREPARE STATEMENT;

END