diff --git a/Problem1_ConsecutiveNumbers.sql b/Problem1_ConsecutiveNumbers.sql new file mode 100644 index 0000000..466a1da --- /dev/null +++ b/Problem1_ConsecutiveNumbers.sql @@ -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 \ No newline at end of file diff --git a/Problem2_PassengerInBus.sql b/Problem2_PassengerInBus.sql new file mode 100644 index 0000000..52f3e18 --- /dev/null +++ b/Problem2_PassengerInBus.sql @@ -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 \ No newline at end of file diff --git a/Problem3_UserActivity.sql b/Problem3_UserActivity.sql new file mode 100644 index 0000000..fac0993 --- /dev/null +++ b/Problem3_UserActivity.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) between 0 and 29 +group by 1 +having count(distinct user_id) >0 \ No newline at end of file diff --git a/Problem4_DynamicPivoting.sql b/Problem4_DynamicPivoting.sql new file mode 100644 index 0000000..2db591b --- /dev/null +++ b/Problem4_DynamicPivoting.sql @@ -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