SELECT p.product_name,o1.product_id,o1.order_id,o1.order_date
FROM Orders o1
INNERJOIN(SELECT product_id,max(order_date)dateFROM Orders
GROUPBY product_id
)o2
ON o1.product_id=o2.product_id AND o1.order_date=o2.dateINNERJOIN Products p
ON o1.product_id=p.product_id
ORDERBY p.product_name asc,o1.product_id asc,o1.order_id
1.3 运行截图
2 餐馆营业额变化增长
2.1 题目内容
2.1.1 基本题目信息
2.1.2 示例输入输出
2.2 示例sql语句
# 一天是不只是一个人光顾的,所以要分组统计出一天所有的营业额# 统计总数要以非分组字段去统计SELECT c1.visited_on,sum(c2.a2) amount,ROUND(avg(c2.a2),2) average_amount
FROM(SELECT visited_on,sum(amount) a1
FROM Customer
GROUPBY visited_on
)c1
CROSSJOIN(SELECT visited_on,sum(amount) a2
FROM Customer
GROUPBY visited_on
)c2
ON TIMESTAMPDIFF(day,c2.visited_on,c1.visited_on)between0and6GROUPBY c1.visited_on
HAVING TIMESTAMPDIFF(day,MIN(c2.visited_on),c1.visited_on)>=6ORDERBY c1.visited_on;
2.3 运行截图
3 买下所有产品的客户
3.1 题目内容
3.1.1 基本题目信息
3.1.2 示例输入输出
3.2 示例sql语句
#一个顾客可能有买同一个商品多次SELECT customer_id
FROM Customer
GROUPBY customer_id
HAVINGcount(distinct product_key)=(SELECTcount(product_key)FROM Product)
举例1
假设我现在有这样一个数据表,它显示了某购物网站在每个城市每个区的销售额:
CREATE TABLE sales(
id INT PRIMARY KEY AUTO_INCREMENT,
city VARCHAR(15),
county VARCHAR(15),
sales_value DECIMAL
);
INSERT INTO sales(city,county,sales_val…