当前位置:网站首页>[force deduction 10 days SQL introduction] Day9 control flow
[force deduction 10 days SQL introduction] Day9 control flow
2022-07-01 07:59:00 【Ly methane】
1393. Capital gains and losses on shares
Stocks surface :
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| stock_name | varchar |
| operation | enum |
| operation_day | int |
| price | int |
+---------------+---------+
(stock_name, day) It's the primary key of this table
operation Column uses an enumeration type , Include :('Sell','Buy')
Each row of this table represents a table named stock_name A stock in operation_day The operating price of this day .
Guarantee every time the stock 'Sell' Before operation , There are corresponding 'Buy' operation .
Write a SQL Query to report the capital gains and losses of each stock .
The capital gains and losses of stocks are all the gains or losses after one or more purchases and sales of stocks .
Just return the results in any order .
Stocks surface :
+---------------+-----------+---------------+--------+
| 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 surface :
+---------------+-------------------+
| stock_name | capital_gain_loss |
+---------------+-------------------+
| Corona Masks | 9500 |
| Leetcode | 8000 |
| Handbags | -23000 |
+---------------+-------------------+
Analysis of the answer
Group by stock name , The operation is to take the negative of buying , Correct the sold , Sum up
SELECT stock_name, sum(IF(operation = 'Buy', -price, price)) capital_gain_loss
FROM Stocks
GROUP BY stock_name
1407. Top ranked travelers
surface :Users
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| name | varchar |
+---------------+---------+
id Is the primary key of the form .name It's the user's name .
surface :Rides
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| user_id | int |
| distance | int |
+---------------+---------+
id Is the primary key of the form .user_id It's from the users of this trip id, The travel distance of this user is distance .
Write a paragraph SQL , Report the travel distance of each user .
The returned result form , With travelled_distance Descending order , If two or more users travel the same distance , Then again name Ascending order .
Users surface :
+------+-----------+
| id | name |
+------+-----------+
| 1 | Alice |
| 2 | Bob |
| 3 | Alex |
| 4 | Donald |
| 7 | Lee |
| 13 | Jonathan |
| 19 | Elvis |
+------+-----------+
Rides surface :
+------+----------+----------+
| 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 surface :
+----------+--------------------+
| name | travelled_distance |
+----------+--------------------+
| Elvis | 450 |
| Lee | 450 |
| Bob | 317 |
| Jonathan | 312 |
| Alex | 222 |
| Alice | 120 |
| Donald | 0 |
+----------+--------------------+
Analysis of the answer
Right link , Press id grouping , Then in descending order of distance , Name ascending
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. Market analysis I
Table: Users
+----------------+---------+
| Column Name | Type |
+----------------+---------+
| user_id | int |
| join_date | date |
| favorite_brand | varchar |
+----------------+---------+
The primary key of this table is user_id.
The table describes the user information of the shopping website , Users can buy and sell goods on this website .
Table: Orders
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| order_id | int |
| order_date | date |
| item_id | int |
| buyer_id | int |
| seller_id | int |
+---------------+---------+
The primary key of this table is order_id. The foreign key is item_id and (buyer_id,seller_id).
Table: Items
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| item_id | int |
| item_brand | varchar |
+---------------+---------+
The primary key of this table is item_id.
Please write a SQL Statement to query the registration date and date of each user 2019 Total number of orders as buyers in .
With In any order Return result table .
Input :
Users surface :
+---------+------------+----------------+
| 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 surface :
+----------+------------+---------+----------+-----------+
| 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 surface :
+---------+------------+
| item_id | item_brand |
+---------+------------+
| 1 | Samsung |
| 2 | Lenovo |
| 3 | LG |
| 4 | HP |
+---------+------------+
Output :
+-----------+------------+----------------+
| 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 |
+-----------+------------+----------------+
Analysis of the answer
Users Table and Orders Table left connection , Press userId grouping ,2019 The sum of
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
边栏推荐
- H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?
- 论文学习——水文时间序列相似性查询的分析与研究
- 2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation
- [skill] create Bat quick open web page
- [software] phantomjs screenshot
- [website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions
- web254
- Minecraft 1.16.5模组开发(五十一) 方块实体 (Tile Entity)
- [batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion
- 【网站架构】一招搞定90%的分布式事务,实打实介绍数据库事务、分布式事务的工作原理应用场景
猜你喜欢

2022年茶艺师(中级)复训题库及答案

2022 mobile crane driver test exercises and online simulation test

How outlook puts together messages with the same discussion

STM32 uses esp01s to go to the cloud, mqtt FX debugging

web254

良心安利万向轮 SolidWorks模型素材网站

Introduction to kubernetes resource objects and common commands (II)

kubernetes资源对象介绍及常用命令(二)

Microsoft stream - how to modify video subtitles

She is the "HR of others" | ones character
随机推荐
AArdio - 【问题】bass库回调时内存增长的问题
Caesar
Basic knowledge of MATLAB
Access report realizes subtotal function
【R语言】两个/N个数据合并merge函数
良心安利万向轮 SolidWorks模型素材网站
Gdip - hatchBrush图案表
TCP/UDP 通信问题整理
Eigen矩阵运算库快速上手
MATLAB之基础知识
Teach you how to apply for domestic trademark online step by step
Redisson uses the full solution - redisson official document + comments (Part 2)
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
力扣每日一题-第32天-1822.数组元素积的符号
The Windows C disk is full
QT -- 1. QT connection database
Scala language learning-07-constructor
base64
Instead of houses, another kind of capital in China is rising
Sorting out tcp/udp communication problems