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 solutions #43

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 consecutiveNumbers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select distinct a.num as ConsecutiveNums from logs a, logs b, logs c
where (a.num = b.num and a.num = c.num) and
(a.id = b.id - 1 and a.id = c.id - 2)
11 changes: 11 additions & 0 deletions passengersInEachBus.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

WITH CTE AS (SELECT P.PASSENGER_ID, MIN(B.ARRIVAL_TIME) AS MIN_BUS_TIME
FROM BUSES B INNER JOIN PASSENGERS P
ON P.ARRIVAL_TIME <= B.ARRIVAL_TIME
GROUP BY P.PASSENGER_ID)

SELECT B.BUS_ID AS BUS_ID, IFNULL(COUNT(P.PASSENGER_ID), 0) PASSENGERS_CNT
FROM BUSES B LEFT JOIN CTE P
ON MIN_BUS_TIME = B.ARRIVAL_TIME
GROUP BY B.BUS_ID ORDER BY 1

12 changes: 12 additions & 0 deletions pivotTable.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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;
-- SELECT @SQL;
SET @SQL = CONCAT('SELECT product_id, ', @SQL, ' FROM PRODUCTS GROUP BY 1');
PREPARE STATEMENT FROM @SQL;
EXECUTE STATEMENT;
DEALLOCATE PREPARE STATEMENT;
END
2 changes: 2 additions & 0 deletions userActivityPast30Days.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select activity_date as day, count(distinct user_id) as active_users
from activity where activity_date >= '2019-06-28' and activity_date <= '2019-07-27' group by activity_date