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

Homework SQL3 #34

Open
wants to merge 4 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
40 changes: 40 additions & 0 deletions activity.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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'
-- GROUP BY activity_date ;

# Using BETWEEN date1 AND date2 (both inclusive)
-- SELECT activity_date AS 'day', COUNT(DISTINCT user_id) AS 'active_users'
-- FROM Activity
-- WHERE activity_date BETWEEN '2019-06-28' AND '2019-07-27'
-- GROUP BY 1;

# Using DATEDIFF(date1, date2)
-- 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;

# Using DATE_SUB(date, INTERVAL expr unit)
-- 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 activity_date;

# Using DATE_ADD(date, INTERVAL expr unit)
-- 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 activity_date;

# Using Having clause ---> GROUP BY clause always comes before Having Clause
-- 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'

SELECT activity_date AS 'day', COUNT(DISTINCT user_id) AS 'active_users'
FROM Activity
GROUP BY activity_date
HAVING activity_date BETWEEN '2019-06-27' AND '2019-07-27';
6 changes: 6 additions & 0 deletions consecutiveNums.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Write your MySQL query statement below
SELECT DISTINCT l1.num AS 'ConsecutiveNums' FROM Logs l1, Logs l2, Logs l3
WHERE l2.id -1 = l1.id
AND l3.id -1 = l2.id
AND l1.num = l2.num
AND l2.num = l3.num;
11 changes: 11 additions & 0 deletions dynamicPivotingTable.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE PROCEDURE PivotProducts()
BEGIN
# Write your MySQL query statement below.
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 stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
33 changes: 33 additions & 0 deletions passengersInBus.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CREATE TABLE Buses (
bus_id INT,
arrival_time INT
);

CREATE TABLE Passengers (
passenger_id INT,
arrival_time INT
);

INSERT INTO Buses (bus_id, arrival_time) VALUES
(1, 2),
(2, 4),
(3, 7);

INSERT INTO Passengers (passenger_id, arrival_time) VALUES
(11, 1),
(12, 5),
(13, 6),
(14, 7);


WITH CTE AS(SELECT p.passenger_id, p.arrival_time, MIN(b.arrival_time) AS bus_arrival_time
FROM Passengers p
INNER JOIN Buses b
ON p.arrival_time <= b.arrival_time
GROUP BY p.passenger_id, p.arrival_time
)
SELECT b.bus_id, COUNT(CTE.bus_arrival_time) AS 'passenger_count'
FROM Buses b
LEFT JOIN CTE
ON b.arrival_time = CTE.bus_arrival_time
GROUP BY b.bus_id;