当前位置:网站首页>[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
边栏推荐
- 【R语言】年龄性别频数匹配 挑选样本 病例对照研究,对年龄性别进行频数匹配
- 【入门】取近似值
- [recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario
- 【网站架构】一招搞定90%的分布式事务,实打实介绍数据库事务、分布式事务的工作原理应用场景
- Oracle create auto increment ID
- Php laraver Wechat payment
- 如何让两融交易更极速
- web254
- [software] phantomjs screenshot
- The H5 page has set the font thickness style, but the wechat access style in Huawei mobile phone doesn't take effect?
猜你喜欢
![[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)](/img/48/de19e8cc007b93a027a906d4d423b2.png)
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)

MATLAB之基础知识

Redisson uses the full solution - redisson official document + comments (Part 2)

The triode is a great invention

QT -- 1. QT connection database

2022 test questions and mock examinations for main principals of hazardous chemicals business units

PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux

AUTOSAR learning record (1) – ECUM_ Init

window c盘满了

奥迪AUDI EDI 项目中供应商需要了解哪些信息?
随机推荐
【力扣10天SQL入门】Day9 控制流
[skill] create Bat quick open web page
She is the "HR of others" | ones character
[untitled]
Introduction to kubernetes resource objects and common commands (II)
Basic number theory -- combinatorial number
Redisson uses the full solution - redisson official documents + comments (Part 2)
Php laraver Wechat payment
【批处理DOS-CMD命令-汇总和小结】-Cmd窗口中常用操作符(<、<<、&<、>、>>、&>、&、&&、||、|、()、;、@)
【入门】截取字符串
力扣每日一题-第32天-1822.数组元素积的符号
The triode is a great invention
Aardio - Shadow Gradient Text
[R language] two /n data merge functions
如何让两融交易更极速
奥迪AUDI EDI 项目中供应商需要了解哪些信息?
2022 electrician (intermediate) recurrent training question bank and answers
Thesis learning -- Analysis and Research on similarity query of hydrological time series
Oracle create auto increment ID
Insufficient executors to build thread pool