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
7 changes: 7 additions & 0 deletions Problem1_ConsecutiveNumbers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Write your MySQL query statement below
select distinct l1.num as ConsecutiveNums
from Logs l1, logs l2, logs l3
where l1.id+1=l2.id
and l2.id+1=l3.id
and l1.num=l2.num
and l2.num=l3.num
13 changes: 13 additions & 0 deletions Problem2_PassengerInBus.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Write your MySQL query statement below
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 1)

select b.bus_id,count(c.passenger_id) as passengers_cnt
from buses b
left join cte c on b.arrival_time=c.btime
group by 1
order by 1
6 changes: 6 additions & 0 deletions Problem3_UserActivity.sql
Original file line number Diff line number Diff line change
@@ -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) between 0 and 29
group by 1
having count(distinct user_id) >0
20 changes: 20 additions & 0 deletions Problem4_DynamicPivoting.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CREATE PROCEDURE PivotProducts()
BEGIN
-- select product_id,
-- sum(if(store='LC_Store',price,null)) as 'LC_Store',
-- sum(if(store='Nozama',price,null)) as 'Nozama',
-- sum(if(store='Shop',price,null)) as 'Shop',
-- sum(if(store='Souq',price,null)) as 'Souq'
-- from products
-- group by 1;
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