1445. Apples and oranges +1393. Stock capital gains and losses +1270. Everyone reporting to the company's CEO +1285. Find the start and end numbers of the continuous interval

Reprint:
Disclaimer: If I violated anyone's rights, please contact me and I will delete it.
Welcome experts to spray me

1445. Apples and Oranges

Link: https://leetcode-cn.com/problems/apples-oranges

Title description

Table: Sales

±--------------±--------+
| Column Name | Type |
±--------------±--- -----+
| sale_date | date |
| fruit | enum |
| sold_num | int |
±--------------±--------+
(sale_date,fruit ) Is the primary key of
the table . The table contains the sales of "apples" and "oranges" in each day.

Write a SQL query to report the difference in the number of apples and oranges sold each day.

The returned result table is sorted by sale_date in the format ('YYYY-MM-DD').

The query result table is shown in the following example:

Sales 表:
±-----------±-----------±------------+
| sale_date | fruit | sold_num |
±-----------±-----------±------------+
| 2020-05-01 | apples | 10 |
| 2020-05-01 | oranges | 8 |
| 2020-05-02 | apples | 15 |
| 2020-05-02 | oranges | 15 |
| 2020-05-03 | apples | 20 |
| 2020-05-03 | oranges | 0 |
| 2020-05-04 | apples | 15 |
| 2020-05-04 | oranges | 16 |
±-----------±-----------±------------+

Result 表:
±-----------±-------------+
| sale_date | diff |
±-----------±-------------+
| 2020-05-01 | 2 |
| 2020-05-02 | 0 |
| 2020-05-03 | 20 |
| 2020-05-04 | -1 |
±-----------±-------------+

On 2020-05-01, 10 apples and 8 oranges were sold (a difference of
10-8 = 2). On 2020-05-02, 15 apples and 15 oranges were sold (a difference of 15-15 = 0). .)
in 2020-05-03, sold 20 apples and oranges 0 (difference of 20 - 0 = 20)
in 2020-05-04, sold 15 apples and 16 oranges (difference of 15 - 16 = -1).

answer

select a.sale_date as sale_date, (a.apple_sum-a.orange_sum) as diff from
(
select sale_date,
sum(case when fruit = 'apples' then sold_num else 0 end) as apple_sum,
sum(case when fruit = 'oranges' then sold_num else 0 end) as orange_sum
from Sales 
group by sale_date
) a
group by sale_date
order by sale_date
select sale_date, sum(case when fruit='apples' then sold_num else -sold_num end) as diff
from Sales
group by sale_date
order by sale_date

1393. Capital gains and losses on stocks

Link: https://leetcode-cn.com/problems/capital-gainloss

Title description

Stocks 表:

±--------------±--------+
| Column Name | Type |
±--------------±--- -----+
| stock_name | varchar |
| operation | enum |
| operation_day | int |
| price | int |
±--------------±-------- +
(stock_name, day) is the primary key of this table. The
operation column uses an enumeration type, including: ('Sell','Buy')
Each row in this table represents a stock named stock_name in operation_day The operating price for this day.
It is guaranteed that there will be a corresponding'Buy' operation before each'Sell' operation of the stock.

Write a SQL query to report the capital gains and losses of each stock.

The capital gains and losses of stocks are all gains or losses after one or more purchases of stocks.

Just return the results in any order.

The format of the SQL query result is shown in the following example:

Stocks 表:
Insert picture description here
Insert picture description here

Leetcode stock was bought for $1,000 on the first day and sold for $9,000 on the fifth day. Capital gains = 9000-1000 = US$8000.
Handbags stock was bought at a price of $30,000 on the 17th day and sold at a price of $7,000 on the 29th day. Capital loss=7000-30000=-23000 USD.
Corona Masks stock was bought at a price of $10 on the first day and sold at a price of $1010 on the third day. Buy it again at the price of 1000 USD on the 4th day and sell it for 500 USD on the 5th day. Finally, it was bought for $1,000 on the 6th day and sold for $10,000 on the 10th day. Capital gains and losses are the sum of capital gains or losses per operation ('Buy'->'Sell') = (1010-10) + (500-1000) + (10000-1000) = 1000-500 + 9000 = 9500 USD.

answer

select stock_name,
sum(case when operation = 'Buy' then -price else price end) as capital_gain_loss
from Stocks
group by stock_name;

1270. Everyone who reports to the CEO of the company

Link: https://leetcode-cn.com/problems/all-people-report-to-the-given-manager

Title description

Employees table: Employees
Insert picture description here
employee_id is the primary key of this table.
In each row of this table, employee_id represents the ID of the employee, employee_name represents the name of the employee, and manager_id represents the line manager of the employee who reports the work.
The CEO of this company is the person whose employee_id = 1.

Use SQL to query the employee_id of all employees who directly or indirectly report to the company's CEO.

Due to the small size of the company, the indirect relationship between managers does not exceed 3 managers.

The results can be returned in any order, without deduplication.

An example of the query result is as follows:
Insert picture description here
the employee_id of the company CEO is 1.
Employees whose employee_id is 2 and 77 directly report to the company CEO.
employee_id is 4 employees who indirectly report to the company CEO 4 --> 2 --> 1.
employee_id is an employee of 7 who indirectly reports to the company CEO 7 --> 4 --> 2 --> 1.
Employees whose employee_id is 3, 8, 9 will not directly or indirectly report to the company CEO.

answer

SELECT e1.employee_id
FROM Employees e1
JOIN Employees e2 ON e1.manager_id = e2.employee_id
JOIN Employees e3 ON e2.manager_id = e3.employee_id
WHERE e1.employee_id != 1 AND e3.manager_id = 1 
 select employee_id from Employees e
 where e.manager_id = 1 and e.employee_id != 1
 or e.manager_id in
 (
     select employee_id from Employees e1
     where e1.manager_id = 1 and e1.employee_id != 1
 )
 or e.manager_id in
 (
     select employee_id from Employees e2
     where e2.manager_id = 1 and e2.employee_id != 1
     or e2.manager_id in
     (
         select employee_id from Employees e3 
         where e3.manager_id = 1 and e3.employee_id != 1
     )
 )
 

1285. Find the start and end numbers of a continuous interval

Link: https://leetcode-cn.com/problems/find-the-start-and-end-number-of-continuous-ranges

Title description

Insert picture description here
id is the primary key of the above table.
Each row of the above table contains an ID in the log table.

Later some IDs were deleted from the Logs table. Write a SQL query to get the start number and end number of the continuous interval in the Logs table.

Sort the query table by start_id.

The query result format is as follows:
Insert picture description here

The result table should contain all the intervals in the Logs table.
From 1 to 3 in the table.
From 4 to 6 are not in the table.
From 7 to 8 in the table.
9 is not in the table.
10 in the table.

answer

# Write your MySQL query statement below
select a.log_id as start_id, b.log_id as end_id from
(
    select log_id from Logs where (log_id -1) not in (select * from Logs)
) a,
(
    select log_id from Logs where (log_id + 1) not in (select * from Logs)
)b
where a.log_id <= b.log_id
group by a.log_id;

Guess you like

Origin blog.csdn.net/qq_45531729/article/details/112297713