iT邦幫忙

0

Day09-LeetCode-SQL啟航~

  • 分享至 

  • xImage
  •  

花了點時間把LeetCode的SQL挑戰第一階跑完,
接著會嘗試分享一些PYTHON寫的小作品,
也許含金量不高,但一定是新手友善的程度。

https://ithelp.ithome.com.tw/upload/images/20221230/20154851eluSCkGjM8.jpg
https://ithelp.ithome.com.tw/upload/images/20221230/20154851nqlr9Bw9XC.jpg

182. Duplicate Emails

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| email       | varchar |
+-------------+---------+
id is the primary key column for this table.
Each row of this table contains an email. The emails will not contain uppercase letters.
 
Write an SQL query to report all the duplicate emails.

Return the result table in any order.

The query result format is in the following example.

Example 1:

Input: 
Person table:
+----+---------+
| id | email   |
+----+---------+
| 1  | a@b.com |
| 2  | c@d.com |
| 3  | a@b.com |
+----+---------+
Output: 
+---------+
| Email   |
+---------+
| a@b.com |
+---------+
Explanation: a@b.com is repeated two times.

這一題要我們找出重複的EMAIL,由於WHERE不能用數學函式,
這次找到了HAVING這個用法來代替WHERE,
就能使用COUNT(Email)>1這樣的條件,
找出出現次數大於1的EMAIL(1次以上就是重複出現)

SELECT Email FROM person
GROUP BY email
HAVING COUNT(Email)>1

1050. Actors and Directors Who Cooperated At Least Three Times

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| actor_id    | int     |
| director_id | int     |
| timestamp   | int     |
+-------------+---------+
timestamp is the primary key column for this table.

Write a SQL query for a report that provides the pairs (actor_id, director_id) where the actor has cooperated with the director at least three times.

Return the result table in any order.

The query result format is in the following example.

Example 1:

Input: 
ActorDirector table:
+-------------+-------------+-------------+
| actor_id    | director_id | timestamp   |
+-------------+-------------+-------------+
| 1           | 1           | 0           |
| 1           | 1           | 1           |
| 1           | 1           | 2           |
| 1           | 2           | 3           |
| 1           | 2           | 4           |
| 2           | 1           | 5           |
| 2           | 1           | 6           |
+-------------+-------------+-------------+
Output: 
+-------------+-------------+
| actor_id    | director_id |
+-------------+-------------+
| 1           | 1           |
+-------------+-------------+
Explanation: The only pair is (1, 1) where they cooperated exactly 3 times.

這一題要找出合作3次以上的演員與導演,
我們馬上用上HAVING去做看看,趁還有記憶,
這邊要在探討一下GROUP BY的特性,
他會去做分組(這邊有actor_id,director_id兩組),
接著不管我們統計 actor_id或director_id或timestamp去>=3
都會得到一樣的結果。

SELECT actor_id,director_id 
FROM ActorDirector A
GROUP BY actor_id,director_id
HAVING COUNT(director_id)>=3

1587. Bank Account Summary II

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| account      | int     |
| name         | varchar |
+--------------+---------+
account is the primary key for this table.
Each row of this table contains the account number of each user in the bank.
There will be no two users having the same name in the table.
 
Table: Transactions

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| trans_id      | int     |
| account       | int     |
| amount        | int     |
| transacted_on | date    |
+---------------+---------+
trans_id is the primary key for this table.
Each row of this table contains all changes made to all accounts.
amount is positive if the user received money and negative if they transferred money.
All accounts start with a balance of 0.

Write an SQL query to report the name and balance of users with a balance higher than 10000. The balance of an account is equal to the sum of the amounts of all transactions involving that account.

Return the result table in any order.

The query result format is in the following example.

Example 1:

Input: 
Users table:
+------------+--------------+
| account    | name         |
+------------+--------------+
| 900001     | Alice        |
| 900002     | Bob          |
| 900003     | Charlie      |
+------------+--------------+
Transactions table:
+------------+------------+------------+---------------+
| trans_id   | account    | amount     | transacted_on |
+------------+------------+------------+---------------+
| 1          | 900001     | 7000       |  2020-08-01   |
| 2          | 900001     | 7000       |  2020-09-01   |
| 3          | 900001     | -3000      |  2020-09-02   |
| 4          | 900002     | 1000       |  2020-09-12   |
| 5          | 900003     | 6000       |  2020-08-07   |
| 6          | 900003     | 6000       |  2020-09-07   |
| 7          | 900003     | -4000      |  2020-09-11   |
+------------+------------+------------+---------------+
Output: 
+------------+------------+
| name       | balance    |
+------------+------------+
| Alice      | 11000      |
+------------+------------+
Explanation: 
Alice's balance is (7000 + 7000 - 3000) = 11000.
Bob's balance is 1000.
Charlie's balance is (6000 + 6000 - 4000) = 8000.

這一題要找出總存款大於10000的用戶,
我們先把兩個表的account欄位做關聯,
再用新學到的HAVING去做加總條件的設定,
這邊提醒count 跟 sum的意義是不一樣的,count是加總數量,sum是加總欄位中的值。

SELECT name,SUM(amount) AS balance    
FROM Users U
LEFT JOIN Transactions T ON U.account =T.account 
GROUP BY name
HAVING SUM(amount)>10000

1084. Sales Analysis III

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| product_id   | int     |
| product_name | varchar |
| unit_price   | int     |
+--------------+---------+
product_id is the primary key of this table.
Each row of this table indicates the name and the price of each product.
Table: Sales

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| seller_id   | int     |
| product_id  | int     |
| buyer_id    | int     |
| sale_date   | date    |
| quantity    | int     |
| price       | int     |
+-------------+---------+
This table has no primary key, it can have repeated rows.
product_id is a foreign key to the Product table.
Each row of this table contains some information about one sale.

Write an SQL query that reports the products that were only sold in the first quarter of 2019. That is, between 2019-01-01 and 2019-03-31 inclusive.

Return the result table in any order.

The query result format is in the following example.

Example 1:

Input: 
Product table:
+------------+--------------+------------+
| product_id | product_name | unit_price |
+------------+--------------+------------+
| 1          | S8           | 1000       |
| 2          | G4           | 800        |
| 3          | iPhone       | 1400       |
+------------+--------------+------------+
Sales table:
+-----------+------------+----------+------------+----------+-------+
| seller_id | product_id | buyer_id | sale_date  | quantity | price |
+-----------+------------+----------+------------+----------+-------+
| 1         | 1          | 1        | 2019-01-21 | 2        | 2000  |
| 1         | 2          | 2        | 2019-02-17 | 1        | 800   |
| 2         | 2          | 3        | 2019-06-02 | 1        | 800   |
| 3         | 3          | 4        | 2019-05-13 | 2        | 2800  |
+-----------+------------+----------+------------+----------+-------+
Output: 
+-------------+--------------+
| product_id  | product_name |
+-------------+--------------+
| 1           | S8           |
+-------------+--------------+
Explanation: 
The product with id 1 was only sold in the spring of 2019.
The product with id 2 was sold in the spring of 2019 but was also sold after the spring of 2019.
The product with id 3 was sold after spring 2019.
We return only product 1 as it is the product that was only sold in the spring of 2019.

這一題要我們找出"僅在第一季販售的商品"
區間是between 2019-01-01 and 2019-03-31,
我們先找能關聯的欄位,找到product_id,
接著用HAVING去設定日期區間即可,
這邊要特別說明product_id(2)中的日期有2019-02-17跟2019-06-02,為什麼不會抓到02-17那筆?
這是因為GROUP BY的特性是將其視為一個群組,所以不會發生那樣的結果。

SELECT P.product_id, P.product_name 
FROM Product P
LEFT JOIN Sales S ON P.product_id =S.product_id
GROUP BY product_id  
HAVING MIN(sale_date) >= "2019-01-01" 
AND MAX(sale_date) <="2019-03-31"

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言