Table of contents
1. The user with the most orders (586)
2. The flow of people in the stadium
Solution 2 (three table method)
1. The user with the most orders (586)
surface: Orders
+-----------------+----------+ | Column Name | Type | +------------- ----+----------+ | order_number | int | | customer_number | int | +-----------------+----- -----+ In SQL, Order_number is the primary key of the table. This table contains information about order ID and customer ID.
Find the customers who placed the most orderscustomer_number
.
After the test case was generated, there happened to be one customer who placed more orders than any other customer.
The query result format is as follows.
Example 1
输入:
Orders 表:
+--------------+-----------------+
| order_number | customer_number |
+--------------+-----------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 3 |
+--------------+-----------------+
输出:
+-----------------+
| customer_number |
+-----------------+
| 3 |
+-----------------+
解释:
customer_number 为 '3' 的顾客有两个订单,比顾客 '1' 或者 '2' 都要多,因为他们只有一个订单。
所以结果是该顾客的 customer_number ,也就是 3 。
Solution one (limit)
First query according to the grouping, use limit to return the order quantity with the largest quantity, and then filter according to the order quantity.
# Write your MySQL query statement below
select customer_number
from orders
group by customer_number
having count(*) = (
select count(*)
from orders
group by customer_number
order by count(*) desc
limit 0,1
)
Solution 2 (dense_rank())
Group first, rank according to the number of orders, and then find out the customer number ranked first
# Write your MySQL query statement below
SELECT customer_number FROM
(SELECT customer_number,
DENSE_RANK() OVER (ORDER BY count(customer_number) DESC) AS `rank`
FROM Orders GROUP BY customer_number) AS t
WHERE t.`rank` = 1;
2. The flow of people in the stadium
surface:Stadium
+---------------+---------+ | Column Name | Type | +---------------+ ---------+ | id | int | | visit_date | date | | people | -+ visit_date is the primary key of the table. The daily flow of people information is recorded in these three columns of information: serial number (id), date (visit_date), and people flow (people) . There is only one row of records per day, and the date increases with the increase of id
Write an SQL query to find three or more consecutive rows where the number of people in each row is greater than or equal to 100
. id
Returns a table of results visit_date
sorted in ascending order.
The query result format is as follows.
Example 1
表:
+------+------------+-----------+
| id | visit_date | people |
+------+------------+-----------+
| 1 | 2017-01-01 | 10 |
| 2 | 2017-01-02 | 109 |
| 3 | 2017-01-03 | 150 |
| 4 | 2017-01-04 | 99 |
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-09 | 188 |
+------+------------+-----------+
输出:
+------+------------+-----------+
| id | visit_date | people |
+------+------------+-----------+
| 5 | 2017-01-05 | 145 |
| 6 | 2017-01-06 | 1455 |
| 7 | 2017-01-07 | 199 |
| 8 | 2017-01-09 | 188 |
+------+------------+-----------+
解释:
id 为 5、6、7、8 的四行 id 连续,并且每行都有 >= 100 的人数记录。
请注意,即使第 7 行和第 8 行的 visit_date 不是连续的,输出也应当包含第 8 行,因为我们只需要考虑 id 连续的记录。
不输出 id 为 2 和 3 的行,因为至少需要三条 id 连续的记录。
解法一(临时表)
First construct a temporary table, which can be effectively grouped according to id-row_number().
Then the second step is more obvious and directly add the condition count>=3
# Write your MySQL query statement below
with t as(
select *,
id-row_number() over (order by id) c
from stadium where people>=100
)
select id,visit_date,people from t where c in(
select c from t group by c having count(c)>=3
)
Solution 2 (three table method)
Put table a in the front, middle, and back respectively, and add the condition that the number of people is greater than or equal to 100
SELECT distinct a.*
FROM stadium as a,stadium as b,stadium as c
where ((a.id = b.id-1 and b.id+1 = c.id) or
(a.id-1 = b.id and a.id+1 = c.id) or
(a.id-1 = c.id and c.id-1 = b.id))
and (a.people>=100 and b.people>=100 and c.people>=100)
order by a.id;