当前位置:网站首页>[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
边栏推荐
- PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux
- Vhost kick & call principle
- Instead of houses, another kind of capital in China is rising
- [R language] age sex frequency matching select samples for case-control study, and perform frequency matching on age and sex
- 2022危险化学品经营单位主要负责人试题及模拟考试
- 【无标题】
- 2022 mobile crane driver test exercises and online simulation test
- Two expressions of string
- [batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)
- Gdip - hatchbrush pattern table
猜你喜欢

The Windows C disk is full

Discussion on several research hotspots of cvpr2022

base64

How to check ad user information?

Access报表实现小计功能

Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure

论文学习——水文时间序列相似性查询的分析与研究

How to make the two financial transactions faster

base64

PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux
随机推荐
2022危险化学品经营单位主要负责人试题及模拟考试
[skill] create Bat quick open web page
Aardio - Method of self constructed geticonhandle
凸印的印刷原理及工艺介绍
Eigen matrix operation Library
【批处理DOS-CMD命令-汇总和小结】-Cmd窗口中常用操作符(<、<<、&<、>、>>、&>、&、&&、||、|、()、;、@)
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
Saving db4i depth camera pictures with MATLAB
The Windows C disk is full
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario
Wang Yingqi, founder of ones, talks to fortune (Chinese version): is there any excellent software in China?
How outlook puts together messages with the same discussion
Thesis learning -- Analysis and Research on similarity query of hydrological time series
ContentType所有类型对比
The database is locked. Is there a solution
Scala语言学习-07-构造器
软键盘高度报错
软件测试方法和技术 - 基础知识概括
力扣——求一组字符中的第一个回文字符
Latex formula code