怎么找网站啊,个人做排行网站,百度推广管理系统,如何用ps设计网页首页目录
1.订单最多的用户(586)
示例 1
解法一(limit)
解法二(dense_rank())
2.体育馆的人流量
示例 1
解法一(临时表)
解法二#xff08;三表法#xff09; 1.订单最多的用户(586)
表: Orders
---------------------------
| Column Name | Type |
---------…目录
1.订单最多的用户(586)
示例 1
解法一(limit)
解法二(dense_rank())
2.体育馆的人流量
示例 1
解法一(临时表)
解法二三表法 1.订单最多的用户(586)
表: Orders
---------------------------
| Column Name | Type |
---------------------------
| order_number | int |
| customer_number | int |
---------------------------
在 SQL 中Order_number是该表的主键。
此表包含关于订单ID和客户ID的信息。查找下了 最多订单 的客户的 customer_number 。
测试用例生成后 恰好有一个客户 比任何其他客户下了更多的订单。
查询结果格式如下所示。
示例 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 。
解法一(limit) 先查询根据分组,用limit返回数量最多的订单数量,然后根据订单数量筛选. # Write your MySQL query statement below
select customer_number
from orders
group by customer_number
having count(*) (select count(*)from ordersgroup by customer_numberorder by count(*) desclimit 0,1
)
解法二(dense_rank()) 先分组,根据订单数量排名,然后查出排名为一的顾客号 # 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.体育馆的人流量
表Stadium
------------------------
| Column Name | Type |
------------------------
| id | int |
| visit_date | date |
| people | int |
------------------------
visit_date 是表的主键
每日人流量信息被记录在这三列信息中序号 (id)、日期 (visit_date)、 人流量 (people)
每天只有一行记录日期随着 id 的增加而增加编写一个 SQL 查询以找出每行的人数大于或等于 100 且 id 连续的三行或更多行记录。
返回按 visit_date 升序排列 的结果表。
查询结果格式如下所示。
示例 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 连续的记录。
解法一(临时表) 首先构造临时表,根据id-row_number()可以有效分组。 然后第二步较为明显直接加条件count3 # Write your MySQL query statement below
with t as(select *,id-row_number() over (order by id) cfrom stadium where people100
)select id,visit_date,people from t where c in(select c from t group by c having count(c)3
)
解法二三表法 把a表分别放前中后再加上人数大于等于100条件 SELECT distinct a.*
FROM stadium as a,stadium as b,stadium as c
where ((a.id b.id-1 and b.id1 c.id) or(a.id-1 b.id and a.id1 c.id) or(a.id-1 c.id and c.id-1 b.id))and (a.people100 and b.people100 and c.people100)
order by a.id;