diff --git a/consecutiveNumbers.sql b/consecutiveNumbers.sql new file mode 100644 index 0000000..5c30060 --- /dev/null +++ b/consecutiveNumbers.sql @@ -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) \ No newline at end of file diff --git a/passengersInEachBus.sql b/passengersInEachBus.sql new file mode 100644 index 0000000..bd1fcc5 --- /dev/null +++ b/passengersInEachBus.sql @@ -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 + diff --git a/pivotTable.sql b/pivotTable.sql new file mode 100644 index 0000000..23b7052 --- /dev/null +++ b/pivotTable.sql @@ -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 \ No newline at end of file diff --git a/userActivityPast30Days.sql b/userActivityPast30Days.sql new file mode 100644 index 0000000..191f13c --- /dev/null +++ b/userActivityPast30Days.sql @@ -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 \ No newline at end of file