Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Sql3 #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions Problem 1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Select distinct(t1.num) as ConsecutiveNums from logs t1, logs t2, logs t3
where t1.id = t2.id+1 and t2.id = t3.id+1 and t1.num = t2.num and t3.num = t2.num

10 changes: 10 additions & 0 deletions Problem 2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
with cte as (
Select p.passenger_id, min(b.arrival_time) as arrival_time from buses b
inner join Passengers p on p.arrival_time<=b.arrival_time
group by passenger_id
)
Select b.bus_id, count(cte.arrival_time) as passengers_cnt
from buses b
left join cte on b.arrival_time = cte.arrival_time
group by bus_id
order by bus_id
4 changes: 4 additions & 0 deletions Problem 3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
select activity_date as day, count(distinct(user_id)) as active_users
from Activity
where activity_date <= "2019-07-27" and activity_date >= "2019-06-28"
group by activity_date
13 changes: 13 additions & 0 deletions Problem 4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE PROCEDURE PivotProducts()
BEGIN
# Write your MySQL query statement below.
SET group_concat_max_len = 1000000;
SET @sql = NULL;
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 product_id');
PREPARE STATEMENT from @sql;
EXECUTE STATEMENT;
DEALLOCATE PREPARE STATEMENT;
END