# 每个用户的所有Sell的price值减去Buy的price值就可以了SELECT e.stock_name,sum(e.price) capital_gain_loss
FROM(SELECT stock_name,operation,operation_day,price*-1 price
FROM Stocks
WHERE operation="Buy"UNIONALLSELECT stock_name,operation,operation_day, price
FROM Stocks
WHERE operation="Sell") e
GROUPBY stock_name
1.3 运行截图
2 排名靠前的旅行者
2.1 题目内容
2.1.1 基本题目信息
2.1.2 示例输入输出
2.2 示例sql语句
SELECT u.name,IFNULL(sum(r.distance),0) travelled_distance
FROM Users u
LEFTJOIN Rides r
ON u.id=r.user_id
GROUPBY u.id,u.name
ORDERBY travelled_distance desc,name asc;
2.3 运行截图
3 市场分析I
3.1 题目内容
3.1.1 基本题目信息1
3.1.2 基本题目信息2
3.1.3 示例输入输出
3.2 示例sql语句
SELECT u.user_id buyer_id,u.join_date join_date,IFNULL(o1.num,0) orders_in_2019
FROM Users u
LEFTJOIN(SELECT buyer_id,count(*) num
FROM Orders
WHERE date_format(order_date,"%Y")=2019GROUPBY buyer_id
) o1
ON u.user_id=o1.buyer_id
GROUPBY u.user_id