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

SQL3_Completed #40

Open
wants to merge 5 commits 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
7 changes: 7 additions & 0 deletions Problem 2: No. Of Passengers in Each Bus
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Write your MySQL query statement below
WITH CTE AS(
SELECT p.passenger_id, p.arrival_time, MIN(b.arrival_time) AS bus_time
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.bus_time) AS 'passengers_cnt' FROM Buses b LEFT JOIN CTE c ON b.arrival_time = c.bus_time
GROUP BY b.bus_id ORDER BY b.bus_id;
13 changes: 13 additions & 0 deletions Problem 4: Dynamic Pivoting of a Table
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 SESSION GROUP_CONCAT_MAX_LEN = 10000000;
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
4 changes: 4 additions & 0 deletions Problem-1 Consecutive Numbers
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Write your MySQL query statement below
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;
25 changes: 25 additions & 0 deletions Problem-3: User Activity
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Write your MySQL query statement below
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'
#We can Use Between
#WHERE activity_date BETWEEN '2019-06-28' AND '2019-07-27'
GROUP BY activity_date; #GROUP BY 1;

#Using DateDiff
SELECT activity_date AS 'day', COUNT(DISTINCT user_id) AS active_users FROM Activity
WHERE DATEDIFF('2019-07-27' , activity_date)>= 0 AND DATEDIFF('2019-07-27' , activity_date)<=29
#WHERE DATEDIFF('2019-07-27' , activity_date) BETWEEN 0 AND 29
GROUP BY 1;

#Using DateSub
SELECT activity_date AS 'day', COUNT(DISTINCT user_id) AS active_users FROM Activity
WHERE activity_date BETWEEN DATE_SUB('2019-07-27', INTERVAL 29 DAY) AND '2019-07-27' GROUP BY 1;

#Using DateAdd
SELECT activity_date AS 'day', COUNT(DISTINCT user_id) AS active_users FROM Activity
WHERE activity_date BETWEEN DATE_ADD('2019-07-27', INTERVAL -29 DAY) AND '2019-07-27' GROUP BY 1;

#Using HAVING
SELECT activity_date AS 'day', COUNT(DISTINCT user_id) AS active_users FROM Activity
GROUP BY activity_date
HAVING activity_date > '2019-06-27' AND activity_date <= '2019-07-27';