当前位置:网站首页>【力扣10天SQL入门】Day9 控制流
【力扣10天SQL入门】Day9 控制流
2022-07-01 07:55:00 【ly甲烷】
1393.股票的资本损益
Stocks 表:
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| stock_name | varchar |
| operation | enum |
| operation_day | int |
| price | int |
+---------------+---------+
(stock_name, day) 是这张表的主键
operation 列使用的是一种枚举类型,包括:('Sell','Buy')
此表的每一行代表了名为 stock_name 的某支股票在 operation_day 这一天的操作价格。
保证股票的每次'Sell'操作前,都有相应的'Buy'操作。
编写一个SQL查询来报告每支股票的资本损益。
股票的资本损益是一次或多次买卖股票后的全部收益或损失。
以任意顺序返回结果即可。
Stocks 表:
+---------------+-----------+---------------+--------+
| stock_name | operation | operation_day | price |
+---------------+-----------+---------------+--------+
| Leetcode | Buy | 1 | 1000 |
| Corona Masks | Buy | 2 | 10 |
| Leetcode | Sell | 5 | 9000 |
| Handbags | Buy | 17 | 30000 |
| Corona Masks | Sell | 3 | 1010 |
| Corona Masks | Buy | 4 | 1000 |
| Corona Masks | Sell | 5 | 500 |
| Corona Masks | Buy | 6 | 1000 |
| Handbags | Sell | 29 | 7000 |
| Corona Masks | Sell | 10 | 10000 |
+---------------+-----------+---------------+--------+
Result 表:
+---------------+-------------------+
| stock_name | capital_gain_loss |
+---------------+-------------------+
| Corona Masks | 9500 |
| Leetcode | 8000 |
| Handbags | -23000 |
+---------------+-------------------+
答案解析
按股票名分组,操作为买入的取负,为卖出的取正,求和
SELECT stock_name, sum(IF(operation = 'Buy', -price, price)) capital_gain_loss
FROM Stocks
GROUP BY stock_name
1407.排名靠前的旅行者
表:Users
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| name | varchar |
+---------------+---------+
id 是该表单主键。name 是用户名字。
表:Rides
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| user_id | int |
| distance | int |
+---------------+---------+
id 是该表单主键。user_id 是本次行程的用户的 id, 而该用户此次行程距离为 distance 。
写一段 SQL , 报告每个用户的旅行距离。
返回的结果表单,以 travelled_distance 降序排列 ,如果有两个或者更多的用户旅行了相同的距离, 那么再以 name 升序排列 。
Users 表:
+------+-----------+
| id | name |
+------+-----------+
| 1 | Alice |
| 2 | Bob |
| 3 | Alex |
| 4 | Donald |
| 7 | Lee |
| 13 | Jonathan |
| 19 | Elvis |
+------+-----------+
Rides 表:
+------+----------+----------+
| id | user_id | distance |
+------+----------+----------+
| 1 | 1 | 120 |
| 2 | 2 | 317 |
| 3 | 3 | 222 |
| 4 | 7 | 100 |
| 5 | 13 | 312 |
| 6 | 19 | 50 |
| 7 | 7 | 120 |
| 8 | 19 | 400 |
| 9 | 7 | 230 |
+------+----------+----------+
Result 表:
+----------+--------------------+
| name | travelled_distance |
+----------+--------------------+
| Elvis | 450 |
| Lee | 450 |
| Bob | 317 |
| Jonathan | 312 |
| Alex | 222 |
| Alice | 120 |
| Donald | 0 |
+----------+--------------------+
答案解析
右联接,按id分组,然后按距离降序,名字升序
SELECT A.name, sum(IFNULL(B.distance, 0)) travelled_distance
FROM Users A LEFT JOIN Rides B ON A.id = B.user_id
GROUP BY A.id
ORDER BY travelled_distance DESC, A.name ASC
1158.市场分析I
Table: Users
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| user_id | int |
| join_date | date |
| favorite_brand | varchar |
+----------------+---------+
此表主键是 user_id。
表中描述了购物网站的用户信息,用户可以在此网站上进行商品买卖。
Table: Orders
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| order_id | int |
| order_date | date |
| item_id | int |
| buyer_id | int |
| seller_id | int |
+---------------+---------+
此表主键是 order_id。外键是 item_id 和(buyer_id,seller_id)。
Table: Items
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| item_id | int |
| item_brand | varchar |
+---------------+---------+
此表主键是 item_id。
请写出一条SQL语句以查询每个用户的注册日期和在 2019 年作为买家的订单总数。
以 任意顺序 返回结果表。
输入:
Users 表:
+---------+------------+----------------+
| user_id | join_date | favorite_brand |
+---------+------------+----------------+
| 1 | 2018-01-01 | Lenovo |
| 2 | 2018-02-09 | Samsung |
| 3 | 2018-01-19 | LG |
| 4 | 2018-05-21 | HP |
+---------+------------+----------------+
Orders 表:
+----------+------------+---------+----------+-----------+
| order_id | order_date | item_id | buyer_id | seller_id |
+----------+------------+---------+----------+-----------+
| 1 | 2019-08-01 | 4 | 1 | 2 |
| 2 | 2018-08-02 | 2 | 1 | 3 |
| 3 | 2019-08-03 | 3 | 2 | 3 |
| 4 | 2018-08-04 | 1 | 4 | 2 |
| 5 | 2018-08-04 | 1 | 3 | 4 |
| 6 | 2019-08-05 | 2 | 2 | 4 |
+----------+------------+---------+----------+-----------+
Items 表:
+---------+------------+
| item_id | item_brand |
+---------+------------+
| 1 | Samsung |
| 2 | Lenovo |
| 3 | LG |
| 4 | HP |
+---------+------------+
输出:
+-----------+------------+----------------+
| buyer_id | join_date | orders_in_2019 |
+-----------+------------+----------------+
| 1 | 2018-01-01 | 1 |
| 2 | 2018-02-09 | 2 |
| 3 | 2018-01-19 | 0 |
| 4 | 2018-05-21 | 0 |
+-----------+------------+----------------+
答案解析
Users表和Orders表左连接,按userId分组,2019年的和
SELECT A.user_id buyer_id, join_date, sum(IF(YEAR(B.order_date) = 2019, 1, 0)) orders_in_2019
FROM users A LEFT JOIN orders B ON A.user_id = B.buyer_id
GROUP BY A.user_id
边栏推荐
- IMDB practice of emotion classification (simplernn, LSTM, Gru)
- STM32 uses esp01s to go to the cloud, mqtt FX debugging
- Significance and measures of source code encryption
- How to make the two financial transactions faster
- Source code analysis of open source API gateway APIs IX
- Gdip - hatchBrush图案表
- How to troubleshoot SharePoint online map network drive failure?
- 力扣每日一题-第32天-1822.数组元素积的符号
- [untitled]
- 三极管是一项伟大的发明
猜你喜欢

redisson使用全解——redisson官方文檔+注釋(上篇)

Gru of RNN
![[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion](/img/ce/6c9e4f2c54710610e8b1f68d6d8088.png)
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion

base64

IMDB practice of emotion classification (simplernn, LSTM, Gru)

Latex table

Custom events of components ②

软件测试方法和技术 - 基础知识概括

038 network security JS

Minecraft 1.16.5模组开发(五十一) 方块实体 (Tile Entity)
随机推荐
Office365 - how to use stream app to watch offline files at any time
Cadence OrCAD capture "network name" is the same, but it is not connected or connected incorrectly. The usage of nodeName of liberation scheme
Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
【mysql学习笔记25】sql语句优化
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
sqlalchemy创建MySQL_Table
【批处理DOS-CMD-汇总】扩展变量-延迟变量cmd /v:on、cmd /v:off、setlocal enabledelayedexpansion、DisableDelayedExpansion
AUTOSAR learning record (1) – ECUM_ Init
组件的自定义事件①
redisson使用全解——redisson官方文档+注释(中篇)
Introduction to kubernetes resource objects and common commands (II)
Redisson uses the complete solution - redisson official documents + Notes (Part 1)
TCP/UDP 通信问题整理
Latex formula code
[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions
[软件] phantomjs屏幕截图
【无标题】
力扣每日一题-第31天-202.快乐数
十大劵商如何开户?另外,手机开户安全么?
【技能】创建.bat快速打开网页