LeetCode-SQL(六)
以下題目均來自力扣
101、1393.股票的資本損益
難度:★★★☆☆
Stocks 表:
+---------------+---------+ | Column Name | Type | +---------------+---------+ | stock_name | varchar | | operation | enum | | operation_day | int | | price | int | +---------------+---------+ (stock_name, day) 是這張表的主鍵 operation 列使用的是一種枚舉類型,包括:('Sell','Buy') 此表的每一行代表了名為 stock_name 的某支股票在 operation_day 這一天的操作價格。 保證股票的每次'Sell'操作前,都有相應的'Buy'操作。編寫一個SQL查詢來報告每支股票的資本損益。
股票的資本損益是一次或多次買賣股票后的全部收益或損失。
以任意順序返回結果即可。
SQL查詢結果的格式如下例所示:
Stocks 表: +---------------+-----------+---------------+--------+ | 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 表: +---------------+-------------------+ | stock_name | capital_gain_loss | +---------------+-------------------+ | Corona Masks | 9500 | | Leetcode | 8000 | | Handbags | -23000 | +---------------+-------------------+ Leetcode 股票在第一天以1000美元的價格買入,在第五天以9000美元的價格賣出。資本收益=9000-1000=8000美元。 Handbags 股票在第17天以30000美元的價格買入,在第29天以7000美元的價格賣出。資本損失=7000-30000=-23000美元。 Corona Masks 股票在第1天以10美元的價格買入,在第3天以1010美元的價格賣出。在第4天以1000美元的價格再次購買,在第5天以500美元的價格出售。最后,它在第6天以1000美元的價格被買走,在第10天以10000美元的價格被賣掉。資本損益是每次(’Buy'->'Sell')操作資本收益或損失的和=(1010-10)+(500-1000)+(10000-1000)=1000-500+9000=9500美元。解答:
用戶分組,分出這個用戶買的總價錢和賣出的總價錢,然后通過轉換為一行,進行相減就可以求出 with t1 as( selectstock_name,operation,sum(price) p fromStocks group by stock_name,operation ), t2 as( selectstock_name,min(case operation when 'buy' then p end) buy_price,min(case operation when 'Sell' then p end) Sell_price fromt1 group by stock_name ) select stock_name,Sell_price-buy_price capital_gain_loss from t2 order by stock_name desc ;102、1389.購買了產品A和產品B卻沒有購買產品C的顧客
難度:★★★☆☆
Customers 表:
+---------------------+---------+ | Column Name | Type | +---------------------+---------+ | customer_id | int | | customer_name | varchar | +---------------------+---------+ customer_id 是這張表的主鍵。 customer_name 是顧客的名稱。Orders 表:
+---------------+---------+ | Column Name | Type | +---------------+---------+ | order_id | int | | customer_id | int | | product_name | varchar | +---------------+---------+ order_id 是這張表的主鍵。 customer_id 是購買了名為 "product_name" 產品顧客的id。請你設計 SQL 查詢來報告購買了產品 A 和產品 B 卻沒有購買產品 C 的顧客的 ID 和姓名( customer_id 和 customer_name ),我們將基于此結果為他們推薦產品 C 。
您返回的查詢結果需要按照 customer_id 排序。
查詢結果如下例所示。
Customers table: +-------------+---------------+ | customer_id | customer_name | +-------------+---------------+ | 1 | Daniel | | 2 | Diana | | 3 | Elizabeth | | 4 | Jhon | +-------------+---------------+Orders table: +------------+--------------+---------------+ | order_id | customer_id | product_name | +------------+--------------+---------------+ | 10 | 1 | A | | 20 | 1 | B | | 30 | 1 | D | | 40 | 1 | C | | 50 | 2 | A | | 60 | 3 | A | | 70 | 3 | B | | 80 | 3 | D | | 90 | 4 | C | +------------+--------------+---------------+Result table: +-------------+---------------+ | customer_id | customer_name | +-------------+---------------+ | 3 | Elizabeth | +-------------+---------------+ 只有 customer_id 為 3 的顧客購買了產品 A 和產品 B ,卻沒有購買產品 C 。解答:
先找購買了產品C的顧客,然后去除這顧客,再從剩下的顧客中找到購買了A和B產品的,分組去重數量等于2就可以了 with o_out_c as( select * from Orders where customer_id not in (select customer_id from Orders where product_name='C') ), tmp as ( select * from o_out_c where product_name in ('A','B') ) select * from Customers where customer_id in (select customer_id from tmp group by customer_id having count(distinct product_name)=2)103、1407.排名靠前的旅行者
難度:★★☆☆☆
表:Users
+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | name | varchar | +---------------+---------+ id 是該表單主鍵。 name 是用戶名字。表:Rides
+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | user_id | int | | distance | int | +---------------+---------+ id 是該表單主鍵。 user_id 是本次行程的用戶的 id, 而該用戶此次行程距離為 distance 。寫一段 SQL , 報告每個用戶的旅行距離。
返回的結果表單,以 travelled_distance 降序排列 ,如果有兩個或者更多的用戶旅行了相同的距離, 那么再以 name 升序排列 。
查詢結果格式如下例所示。
Users 表: +------+-----------+ | id | name | +------+-----------+ | 1 | Alice | | 2 | Bob | | 3 | Alex | | 4 | Donald | | 7 | Lee | | 13 | Jonathan | | 19 | Elvis | +------+-----------+Rides 表: +------+----------+----------+ | 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 表: +----------+--------------------+ | name | travelled_distance | +----------+--------------------+ | Elvis | 450 | | Lee | 450 | | Bob | 317 | | Jonathan | 312 | | Alex | 222 | | Alice | 120 | | Donald | 0 | +----------+--------------------+ Elvis 和 Lee 旅行了 450 英里,Elvis 是排名靠前的旅行者,因為他的名字在字母表上的排序比 Lee 更小。 Bob, Jonathan, Alex 和 Alice 只有一次行程,我們只按此次行程的全部距離對他們排序。 Donald 沒有任何行程, 他的旅行距離為 0。解答:
每個旅行者,所以用戶表所有人都應該在,這里使用左連接(left join),然后根據姓名進行分組求和,求和里面設置如果為null的為0,最后進行排序 selectname,sum(ifnull(distance,0)) travelled_distance fromUsers u left joinRides r onu.id=r.user_id group byname order bytravelled_distance desc,name asc ;104、1412.查找成績處于中游的學生
難度:★★★★★
表: Student
+---------------------+---------+ | Column Name | Type | +---------------------+---------+ | student_id | int | | student_name | varchar | +---------------------+---------+ student_id 是該表主鍵. student_name 學生名字.表: Exam
+---------------+---------+ | Column Name | Type | +---------------+---------+ | exam_id | int | | student_id | int | | score | int | +---------------+---------+ (exam_id, student_id) 是該表主鍵. 學生 student_id 在測驗 exam_id 中得分為 score.成績處于中游的學生是指至少參加了一次測驗, 且得分既不是最高分也不是最低分的學生。
寫一個 SQL 語句,找出在 所有 測驗中都處于中游的學生 (student_id, student_name)。
不要返回從來沒有參加過測驗的學生。返回結果表按照 student_id 排序。
查詢結果格式如下。
Student 表: +-------------+---------------+ | student_id | student_name | +-------------+---------------+ | 1 | Daniel | | 2 | Jade | | 3 | Stella | | 4 | Jonathan | | 5 | Will | +-------------+---------------+Exam 表: +------------+--------------+-----------+ | exam_id | student_id | score | +------------+--------------+-----------+ | 10 | 1 | 70 | | 10 | 2 | 80 | | 10 | 3 | 90 | | 20 | 1 | 80 | | 30 | 1 | 70 | | 30 | 3 | 80 | | 30 | 4 | 90 | | 40 | 1 | 60 | | 40 | 2 | 70 | | 40 | 4 | 80 | +------------+--------------+-----------+Result 表: +-------------+---------------+ | student_id | student_name | +-------------+---------------+ | 2 | Jade | +-------------+---------------+對于測驗 1: 學生 1 和 3 分別獲得了最低分和最高分。 對于測驗 2: 學生 1 既獲得了最高分, 也獲得了最低分。 對于測驗 3 和 4: 學生 1 和 4 分別獲得了最低分和最高分。 學生 2 和 5 沒有在任一場測驗中獲得了最高分或者最低分。 因為學生 5 從來沒有參加過任何測驗, 所以他被排除于結果表。 由此, 我們僅僅返回學生 2 的信息。解答:
排序開窗,根據分數正著排一次,然后反著再排一次,再做一個標志轉換,排名不為1的都為0 selectexam_id,student_id,score,if(dense_rank() over(partition by exam_id order by score desc)=1,1,0) dr,if(dense_rank() over(partition by exam_id order by score)=1,1,0) ar fromExam ; 連接學生表,然后對學生id求分組,條件為sum(dr)=0和sum(ar)=0的 selectt.student_id,student_name from (selectexam_id,student_id,score,if(dense_rank() over(partition by exam_id order by score desc)=1,1,0) dr,if(dense_rank() over(partition by exam_id order by score)=1,1,0) ar fromExam) t join Student s ont.student_id=s.student_id group byt.student_id,student_name havingsum(dr)=0 andsum(ar)=0 ;105、1421.凈現值查詢
難度:★★☆☆☆
表: NPV
+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | year | int | | npv | int | +---------------+---------+ (id, year) 是該表主鍵. 該表有每一筆存貨的年份, id 和對應凈現值的信息.表: Queries
+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | year | int | +---------------+---------+ (id, year) 是該表主鍵. 該表有每一次查詢所對應存貨的 id 和年份的信息.寫一個 SQL, 找到 Queries 表中每一次查詢的凈現值.
結果表沒有順序要求.
查詢結果的格式如下所示:
NPV 表: +------+--------+--------+ | id | year | npv | +------+--------+--------+ | 1 | 2018 | 100 | | 7 | 2020 | 30 | | 13 | 2019 | 40 | | 1 | 2019 | 113 | | 2 | 2008 | 121 | | 3 | 2009 | 12 | | 11 | 2020 | 99 | | 7 | 2019 | 0 | +------+--------+--------+Queries 表: +------+--------+ | id | year | +------+--------+ | 1 | 2019 | | 2 | 2008 | | 3 | 2009 | | 7 | 2018 | | 7 | 2019 | | 7 | 2020 | | 13 | 2019 | +------+--------+結果表: +------+--------+--------+ | id | year | npv | +------+--------+--------+ | 1 | 2019 | 113 | | 2 | 2008 | 121 | | 3 | 2009 | 12 | | 7 | 2018 | 0 | | 7 | 2019 | 0 | | 7 | 2020 | 30 | | 13 | 2019 | 40 | +------+--------+--------+(7, 2018)的凈現值不在 NPV 表中, 我們把它看作是 0. 所有其它查詢的凈現值都能在 NPV 表中找到.解答:
左連接,判斷null為0 select q.id,q.year,ifnull(npv,0) npv from Queries q left join NPV n on q.id=n.id and q.year=n.year106、1435.繪制會話柱狀圖
難度:★★☆☆☆
表:Sessions
+---------------------+---------+ | Column Name | Type | +---------------------+---------+ | session_id | int | | duration | int | +---------------------+---------+ session_id 是該表主鍵 duration 是用戶訪問應用的時間, 以秒為單位你想知道用戶在你的 app 上的訪問時長情況。因此決定統計訪問時長區間分別為 “[0-5>”, “[5-10>”, “[10-15>” 和 “15 or more” (單位:分鐘)的會話數量,并以此繪制柱狀圖。
寫一個SQL查詢來報告(訪問時長區間,會話總數)。結果可用任何順序呈現
下方為查詢的輸出格式:
Sessions 表: +-------------+---------------+ | session_id | duration | +-------------+---------------+ | 1 | 30 | | 2 | 199 | | 3 | 299 | | 4 | 580 | | 5 | 1000 | +-------------+---------------+Result 表: +--------------+--------------+ | bin | total | +--------------+--------------+ | [0-5> | 3 | | [5-10> | 1 | | [10-15> | 0 | | 15 or more | 1 | +--------------+--------------+對于 session_id 1,2 和 3 ,它們的訪問時間大于等于 0 分鐘且小于 5 分鐘。 對于 session_id 4,它的訪問時間大于等于 5 分鐘且小于 10 分鐘。 沒有會話的訪問時間大于等于 10 分鐘且小于 15 分鐘。 對于 session_id 5, 它的訪問時間大于等于 15 分鐘。解答:
select '[0-5>' as bin,count(duration) as total from Sessions where duration < 300 union select '[5-10>' as bin,count(duration) as total from Sessions where duration >= 300 and duration < 600 union select '[10-15>' as bin,count(duration) as total from Sessions where duration >= 600 and duration < 900 union select '15 or more' as bin,count(duration) as total from Sessions where duration > 900107、1440.計算布爾表達式的值
難度:★★★★☆
表 Variables:
+---------------+---------+ | Column Name | Type | +---------------+---------+ | name | varchar | | value | int | +---------------+---------+ name 是該表主鍵. 該表包含了存儲的變量及其對應的值.表 Expressions:
+---------------+---------+ | Column Name | Type | +---------------+---------+ | left_operand | varchar | | operator | enum | | right_operand | varchar | +---------------+---------+ (left_operand, operator, right_operand) 是該表主鍵. 該表包含了需要計算的布爾表達式. operator 是枚舉類型, 取值于('<', '>', '=') left_operand 和 right_operand 的值保證存在于 Variables 表單中.寫一個 SQL 查詢, 以計算表 Expressions 中的布爾表達式.
返回的結果表沒有順序要求.
查詢結果格式如下例所示.
Variables 表: +------+-------+ | name | value | +------+-------+ | x | 66 | | y | 77 | +------+-------+Expressions 表: +--------------+----------+---------------+ | left_operand | operator | right_operand | +--------------+----------+---------------+ | x | > | y | | x | < | y | | x | = | y | | y | > | x | | y | < | x | | x | = | x | +--------------+----------+---------------+Result 表: +--------------+----------+---------------+-------+ | left_operand | operator | right_operand | value | +--------------+----------+---------------+-------+ | x | > | y | false | | x | < | y | true | | x | = | y | false | | y | > | x | true | | y | < | x | false | | x | = | x | true | +--------------+----------+---------------+-------+ 如上所示, 你需要通過使用 Variables 表來找到 Expressions 表中的每一個布爾表達式的值.解答:
Expressions表與Variables表進行連接兩次,然后使用case when then else end去判斷即可 with tmp as( select left_operand,v1.value left_value,operator,right_operand,v2.value right_value from Expressions e left join Variables v1 on e.left_operand=v1.name left join Variables v2 on e.right_operand=v2.name ) select left_operand, operator, right_operand, case when operator='>' and left_value>right_value then 'true'when operator='<' and left_value<right_value then 'true'when operator='=' and left_value=right_value then 'true'else 'false' end as valuefrom tmp108、1445.橘子和蘋果
難度:★★★☆☆
表: Sales
+---------------+---------+ | Column Name | Type | +---------------+---------+ | sale_date | date | | fruit | enum | | sold_num | int | +---------------+---------+ (sale_date,fruit) 是該表主鍵. 該表包含了每一天中"蘋果" 和 "桔子"的銷售情況.寫一個 SQL 查詢, 報告每一天 蘋果 和 桔子 銷售的數目的差異.
返回的結果表, 按照格式為 (‘YYYY-MM-DD’) 的 sale_date 排序.
查詢結果表如下例所示:
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 | +------------+--------------+在 2020-05-01, 賣了 10 個蘋果 和 8 個桔子 (差異為 10 - 8 = 2). 在 2020-05-02, 賣了 15 個蘋果 和 15 個桔子 (差異為 15 - 15 = 0). 在 2020-05-03, 賣了 20 個蘋果 和 0 個桔子 (差異為 20 - 0 = 20). 在 2020-05-04, 賣了 15 個蘋果 和 16 個桔子 (差異為 15 - 16 = -1).解答:
select s1.sale_date sale_date,s1.sold_num-s2.sold_num diff # 求差即可 from Sales s1 join Sales s2 on s1.sale_date=s2.sale_date # 自連接在一起,方便計算 where s1.fruit='apples' and s2.fruit='oranges' # 讓蘋果和橘子在一行上 order by sale_date109、1454.活躍用戶
難度:★★★☆☆
表 Accounts:
+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | name | varchar | +---------------+---------+ id 是該表主鍵. 該表包含賬戶 id 和賬戶的用戶名.表 Logins:
+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | login_date | date | +---------------+---------+ 該表無主鍵, 可能包含重復項. 該表包含登錄用戶的賬戶 id 和登錄日期. 用戶也許一天內登錄多次.寫一個 SQL 查詢, 找到活躍用戶的 id 和 name.
活躍用戶是指那些至少連續 5 天登錄賬戶的用戶.
返回的結果表按照 id 排序.
結果表格式如下例所示
Accounts 表: +----+----------+ | id | name | +----+----------+ | 1 | Winston | | 7 | Jonathan | +----+----------+Logins 表: +----+------------+ | id | login_date | +----+------------+ | 7 | 2020-05-30 | | 1 | 2020-05-30 | | 7 | 2020-05-31 | | 7 | 2020-06-01 | | 7 | 2020-06-02 | | 7 | 2020-06-02 | | 7 | 2020-06-03 | | 1 | 2020-06-07 | | 7 | 2020-06-10 | +----+------------+Result 表: +----+----------+ | id | name | +----+----------+ | 7 | Jonathan | +----+----------+ id = 1 的用戶 Winston 僅僅在不同的 2 天內登錄了 2 次, 所以, Winston 不是活躍用戶. id = 7 的用戶 Jonathon 在不同的 6 天內登錄了 7 次, , 6 天中有 5 天是連續的, 所以, Jonathan 是活躍用戶.解答:
# 思路: # 對每個用戶和日期進行排序,然后用日期去減排名,最后對用戶和相減后的日期進行分組,如果數量大于等于5的則是連續登錄 with tmp as( selectl.id,l.login_date,a.name fromLogins l left joinAccounts a onl.id=a.id # 兩表連接,使得id和name在一起 group byl.id,l.login_date,a.name # 分組,為了使每個用戶每天只有一次 ), tmp1 as( selectid,login_date,name,rank() over(partition by id order by login_date) rk # 排序 fromtmp ), tmp2 as( selectid,name,login_date,date_add(login_date,interval -rk day) dt # 求差 fromtmp1 ) selectdistinct id, # 去重name fromtmp2 group byid,dt # 分組id和日期 hvaingcount(*)>=5 # 數量大于等于5的說明連續登錄5天 order byid ;110、1459.矩形面積
難度:★★★★☆
表: Points
+---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | x_value | int | | y_value | int | +---------------+---------+ id 是該表主鍵 每個點都用二維坐標 (x_value, y_value) 表示寫一個 SQL 語句,報告由表中任意兩點可以形成的所有 邊與坐標軸平行 且 面積不為零 的矩形。
結果表中的每一行包含三列 (p1, p2, area) 如下:
- p1 和 p2 是矩形兩個對角的 id
- 矩形的面積由列 area 表示
請按照面積 area 大小降序排列;如果面積相同的話, 則按照 p1 升序排序;若仍相同,則按 p2 升序排列。
查詢結果如下例所示:
Points 表: +----------+-------------+-------------+ | id | x_value | y_value | +----------+-------------+-------------+ | 1 | 2 | 7 | | 2 | 4 | 8 | | 3 | 2 | 10 | +----------+-------------+-------------+Result 表: +----------+-------------+-------------+ | p1 | p2 | area | +----------+-------------+-------------+ | 2 | 3 | 4 | | 1 | 2 | 2 | +----------+-------------+-------------+ https://assets.leetcode.com/uploads/2021/03/12/rect.png p1 = 2 且 p2 = 3 時, 面積等于 |4-2| * |8-10| = 4 p1 = 1 且 p2 = 2 時, 面積等于 ||2-4| * |7-8| = 2 p1 = 1 且 p2 = 3 時, 是不可能為矩形的, 面積等于 0解答:
SELECTp1.id P1,p2.id P2,(ABS(p1.x_value - p2.x_value) * ABS(p1.y_value - p2.y_value)) AS AREA FROMPoints p1 JOINPoints p2 ONp1.id < p2.id HAVINGAREA <> 0 ORDER BYAREA DESC,P1,P2 ;111、1468.計算稅后工資
難度:★★★★☆
Salaries 表:
+---------------+---------+ | Column Name | Type | +---------------+---------+ | company_id | int | | employee_id | int | | employee_name | varchar | | salary | int | +---------------+---------+ (company_id, employee_id) 是這個表的主鍵 這個表包括員工的company id, id, name 和 salary寫一條查詢 SQL 來查找每個員工的稅后工資
每個公司的稅率計算依照以下規則
- 如果這個公司員工最高工資不到 1000 ,稅率為 0%
- 如果這個公司員工最高工資在 1000 到 10000 之間,稅率為 24%
- 如果這個公司員工最高工資大于 10000 ,稅率為 49%
按任意順序返回結果,稅后工資結果取整
結果表格式如下例所示:
Salaries 表: +------------+-------------+---------------+--------+ | company_id | employee_id | employee_name | salary | +------------+-------------+---------------+--------+ | 1 | 1 | Tony | 2000 | | 1 | 2 | Pronub | 21300 | | 1 | 3 | Tyrrox | 10800 | | 2 | 1 | Pam | 300 | | 2 | 7 | Bassem | 450 | | 2 | 9 | Hermione | 700 | | 3 | 7 | Bocaben | 100 | | 3 | 2 | Ognjen | 2200 | | 3 | 13 | Nyancat | 3300 | | 3 | 15 | Morninngcat | 7777 | +------------+-------------+---------------+--------+Result 表: +------------+-------------+---------------+--------+ | company_id | employee_id | employee_name | salary | +------------+-------------+---------------+--------+ | 1 | 1 | Tony | 1020 | | 1 | 2 | Pronub | 10863 | | 1 | 3 | Tyrrox | 5508 | | 2 | 1 | Pam | 300 | | 2 | 7 | Bassem | 450 | | 2 | 9 | Hermione | 700 | | 3 | 7 | Bocaben | 76 | | 3 | 2 | Ognjen | 1672 | | 3 | 13 | Nyancat | 2508 | | 3 | 15 | Morninngcat | 5911 | +------------+-------------+---------------+--------+ 對于公司 1 ,最高工資是 21300 ,其每個員工的稅率為 49% 對于公司 2 ,最高工資是 700 ,其每個員工稅率為 0% 對于公司 3 ,最高工資是 7777 ,其每個員工稅率是 24% 稅后工資計算 = 工資 - ( 稅率 / 100)*工資 對于上述案例,Morninngcat 的稅后工資 = 7777 - 7777 * ( 24 / 100) = 7777 - 1866.48 = 5910.52 ,取整為 5911解答:
with tmp as( # 臨時表一,求出每個salary對應的稅率 selectcompany_id,employee_id,employee_name,salary,case when salary<1000 then 0when salary>1000 and salary<=10000 then 0.24when salary>10000 then 0.49 end as lv # 每個salary對應的稅率 from Salaries ), tmp1 as( # 臨時表二,求出每個公司中稅率最高的 select company_id, employee_id, employee_name, salary, max(lv) over(partition by company_id) lv1 # 稅率最高的 from tmp ) select company_id, employee_id, employee_name, round(salary-salary*lv1,0) salary # 取整計算稅后工資 from tmp1 :112、1479.周內每天的銷售情況
難度:★★★★★
表:Orders
+---------------+---------+ | Column Name | Type | +---------------+---------+ | order_id | int | | customer_id | int | | order_date | date | | item_id | varchar | | quantity | int | +---------------+---------+ (order_id, item_id) 是該表主鍵 該表包含了訂單信息 order_date 是id為 item_id 的商品被id為 customer_id 的消費者訂購的日期.表:Items
+---------------------+---------+ | Column Name | Type | +---------------------+---------+ | item_id | varchar | | item_name | varchar | | item_category | varchar | +---------------------+---------+ item_id 是該表主鍵 item_name 是商品的名字 item_category 是商品的類別你是企業主,想要獲得分類商品和周內每天的銷售報告。
寫一個SQL語句,報告 周內每天 每個商品類別下訂購了多少單位。
返回結果表單 按商品類別排序 。
查詢結果格式如下例所示:
Orders 表: +------------+--------------+-------------+--------------+-------------+ | order_id | customer_id | order_date | item_id | quantity | +------------+--------------+-------------+--------------+-------------+ | 1 | 1 | 2020-06-01 | 1 | 10 | | 2 | 1 | 2020-06-08 | 2 | 10 | | 3 | 2 | 2020-06-02 | 1 | 5 | | 4 | 3 | 2020-06-03 | 3 | 5 | | 5 | 4 | 2020-06-04 | 4 | 1 | | 6 | 4 | 2020-06-05 | 5 | 5 | | 7 | 5 | 2020-06-05 | 1 | 10 | | 8 | 5 | 2020-06-14 | 4 | 5 | | 9 | 5 | 2020-06-21 | 3 | 5 | +------------+--------------+-------------+--------------+-------------+Items 表: +------------+----------------+---------------+ | item_id | item_name | item_category | +------------+----------------+---------------+ | 1 | LC Alg. Book | Book | | 2 | LC DB. Book | Book | | 3 | LC SmarthPhone | Phone | | 4 | LC Phone 2020 | Phone | | 5 | LC SmartGlass | Glasses | | 6 | LC T-Shirt XL | T-Shirt | +------------+----------------+---------------+Result 表: +------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+ | Category | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday | +------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+ | Book | 20 | 5 | 0 | 0 | 10 | 0 | 0 | | Glasses | 0 | 0 | 0 | 0 | 5 | 0 | 0 | | Phone | 0 | 0 | 5 | 1 | 0 | 0 | 10 | | T-Shirt | 0 | 0 | 0 | 0 | 0 | 0 | 0 | +------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+ 在周一(2020-06-01, 2020-06-08),Book分類(ids: 1, 2)下,總共銷售了20個單位(10 + 10) 在周二(2020-06-02),Book分類(ids: 1, 2)下,總共銷售了5個單位 在周三(2020-06-03),Phone分類(ids: 3, 4)下,總共銷售了5個單位 在周四(2020-06-04),Phone分類(ids: 3, 4)下,總共銷售了1個單位 在周五(2020-06-05),Book分類(ids: 1, 2)下,總共銷售了10個單位,Glasses分類(ids: 5)下,總共銷售了5個單位 在周六, 沒有商品銷售 在周天(2020-06-14, 2020-06-21),Phone分類(ids: 3, 4)下,總共銷售了10個單位(5 + 5) 沒有銷售 T-Shirt 類別的商品解答:
with tmp as( select i.item_category,o.item_id, dayname(order_date) as wk,quantity from Orders o right join Items i # 臨時表,用orders表右連接items表,因為需要的是items里面的全部類別 on o.item_id=i.item_id # 順便使用dayname函數求出日期是星期幾 ) select item_category Category, sum(if(wk='Monday',quantity,0)) as Monday, # 分組要使用聚合函數,日期為周一的進行相加 sum(if(wk='Tuesday',quantity,0)) as Tuesday, # 分組要使用聚合函數,日期為周二的進行相加 sum(if(wk='Wednesday',quantity,0)) as Wednesday, # 分組要使用聚合函數,日期為周三的進行相加 sum(if(wk='Thursday',quantity,0)) as Thursday, # 分組要使用聚合函數,日期為周四的進行相加 sum(if(wk='Friday',quantity,0)) as Friday, # 分組要使用聚合函數,日期為周五的進行相加 sum(if(wk='Saturday',quantity,0)) as Saturday, # 分組要使用聚合函數,日期為周六的進行相加 sum(if(wk='Sunday',quantity,0)) as Sunday # 分組要使用聚合函數,日期為周天的進行相加 from tmp group by item_category # 按照要求對類別進行分組 order by Category # 按照要求對類別進行排序擴展:
# 日期函數 # 1、直接求日期為星期幾 : dayname(date) select dayname('2022-01-18') Tuesday # 2、求日期為星期幾:dayofweek(date) 1代表周日,2代表周一。。。。依次類推 select dayofweek('2022-01-18') 3 # 3、求日期為星期幾:weekday(date) 0代表周一,1代表周二。。。。依次類推 select weekday('2022-01-18') 1113、1484.按日期分組銷售產品
難度:★★☆☆☆
表 Activities:
+-------------+---------+ | 列名 | 類型 | +-------------+---------+ | sell_date | date | | product | varchar | +-------------+---------+ 此表沒有主鍵,它可能包含重復項。 此表的每一行都包含產品名稱和在市場上銷售的日期。編寫一個 SQL 查詢來查找每個日期、銷售的不同產品的數量及其名稱。
每個日期的銷售產品名稱應按詞典序排列。
返回按 sell_date 排序的結果表。
查詢結果格式如下例所示。
Activities 表: +------------+-------------+ | sell_date | product | +------------+-------------+ | 2020-05-30 | Headphone | | 2020-06-01 | Pencil | | 2020-06-02 | Mask | | 2020-05-30 | Basketball | | 2020-06-01 | Bible | | 2020-06-02 | Mask | | 2020-05-30 | T-Shirt | +------------+-------------+Result 表: +------------+----------+------------------------------+ | sell_date | num_sold | products | +------------+----------+------------------------------+ | 2020-05-30 | 3 | Basketball,Headphone,T-shirt | | 2020-06-01 | 2 | Bible,Pencil | | 2020-06-02 | 1 | Mask | +------------+----------+------------------------------+ 對于2020-05-30,出售的物品是 (Headphone, Basketball, T-shirt),按詞典序排列,并用逗號 ',' 分隔。 對于2020-06-01,出售的物品是 (Pencil, Bible),按詞典序排列,并用逗號分隔。 對于2020-06-02,出售的物品是 (Mask),只需返回該物品名。解答:
SELECT sell_date AS 'sell_date',COUNT(DISTINCT product) AS 'num_sold', # 會重復,去重GROUP_CONCAT(DISTINCT product ORDER BY product ASC #按照字典序排列,升序SEPARATOR ',') #用','分隔AS 'products' #組內拼接 FROM Activities GROUP BY sell_date ORDER BY sell_date;114、1495.上月播放的兒童適宜電影
難度:★★☆☆☆
表: TVProgram
+---------------+---------+ | Column Name | Type | +---------------+---------+ | program_date | date | | content_id | int | | channel | varchar | +---------------+---------+ (program_date, content_id) 是該表主鍵. 該表包含電視上的節目信息. content_id 是電視一些頻道上的節目的 id.表: Content
+------------------+---------+ | Column Name | Type | +------------------+---------+ | content_id | varchar | | title | varchar | | Kids_content | enum | | content_type | varchar | +------------------+---------+ content_id 是該表主鍵. Kids_content 是枚舉類型, 取值為('Y', 'N'), 其中: 'Y' 表示兒童適宜內容, 而'N'表示兒童不宜內容. content_type 表示內容的類型, 比如電影, 電視劇等.寫一個 SQL 語句, 報告在 2020 年 6 月份播放的兒童適宜電影的去重電影名.
返回的結果表單沒有順序要求.
查詢結果的格式如下例所示.
TVProgram 表: +--------------------+--------------+-------------+ | program_date | content_id | channel | +--------------------+--------------+-------------+ | 2020-06-10 08:00 | 1 | LC-Channel | | 2020-05-11 12:00 | 2 | LC-Channel | | 2020-05-12 12:00 | 3 | LC-Channel | | 2020-05-13 14:00 | 4 | Disney Ch | | 2020-06-18 14:00 | 4 | Disney Ch | | 2020-07-15 16:00 | 5 | Disney Ch | +--------------------+--------------+-------------+Content 表: +------------+----------------+---------------+---------------+ | content_id | title | Kids_content | content_type | +------------+----------------+---------------+---------------+ | 1 | Leetcode Movie | N | Movies | | 2 | Alg. for Kids | Y | Series | | 3 | Database Sols | N | Series | | 4 | Aladdin | Y | Movies | | 5 | Cinderella | Y | Movies | +------------+----------------+---------------+---------------+Result 表: +--------------+ | title | +--------------+ | Aladdin | +--------------+ "Leetcode Movie" 是兒童不宜的電影. "Alg. for Kids" 不是電影. "Database Sols" 不是電影 "Alladin" 是電影, 兒童適宜, 并且在 2020 年 6 月份播放. "Cinderella" 不在 2020 年 6 月份播放.解答:
selectdistinct # 6、去重電影名c.title from Content c left join # 1、要的是title,所以左連接TVProgram t on c.content_id=t.content_id # 2、連接條件 where c.content_type='Movies' # 3、條件判斷,是電影的 andc.kids_content='Y' # 4、條件判斷,是適宜兒童的 and date_format(t.program_date,'%Y-%m')='2020-06' # 5、條件判斷,是指定日期的 ;115、1501.可以放心投資的國家
難度:★★★☆☆
表 Person:
+----------------+---------+ | Column Name | Type | +----------------+---------+ | id | int | | name | varchar | | phone_number | varchar | +----------------+---------+ id 是該表主鍵. 該表每一行包含一個人的名字和電話號碼. 電話號碼的格式是:'xxx-yyyyyyy', 其中xxx是國家碼(3個字符), yyyyyyy是電話號碼(7個字符), x和y都表示數字. 同時, 國家碼和電話號碼都可以包含前導0.表 Country:
+----------------+---------+ | Column Name | Type | +----------------+---------+ | name | varchar | | country_code | varchar | +----------------+---------+ country_code是該表主鍵. 該表每一行包含國家名和國家碼. country_code的格式是'xxx', x是數字.表 Calls:
+-------------+------+ | Column Name | Type | +-------------+------+ | caller_id | int | | callee_id | int | | duration | int | +-------------+------+ 該表無主鍵, 可能包含重復行. 每一行包含呼叫方id, 被呼叫方id和以分鐘為單位的通話時長. caller_id != callee_id一家電信公司想要投資新的國家. 該公司想要投資的國家是: 該國的平均通話時長要嚴格地大于全球平均通話時長.
寫一段 SQL, 找到所有該公司可以投資的國家.
返回的結果表沒有順序要求.
查詢的結果格式如下例所示
Person 表: +----+----------+--------------+ | id | name | phone_number | +----+----------+--------------+ | 3 | Jonathan | 051-1234567 | | 12 | Elvis | 051-7654321 | | 1 | Moncef | 212-1234567 | | 2 | Maroua | 212-6523651 | | 7 | Meir | 972-1234567 | | 9 | Rachel | 972-0011100 | +----+----------+--------------+Country 表: +----------+--------------+ | name | country_code | +----------+--------------+ | Peru | 051 | | Israel | 972 | | Morocco | 212 | | Germany | 049 | | Ethiopia | 251 | +----------+--------------+Calls 表: +-----------+-----------+----------+ | caller_id | callee_id | duration | +-----------+-----------+----------+ | 1 | 9 | 33 | | 2 | 9 | 4 | | 1 | 2 | 59 | | 3 | 12 | 102 | | 3 | 12 | 330 | | 12 | 3 | 5 | | 7 | 9 | 13 | | 7 | 1 | 3 | | 9 | 7 | 1 | | 1 | 7 | 7 | +-----------+-----------+----------+Result 表: +----------+ | country | +----------+ | Peru | +----------+ 國家Peru的平均通話時長是 (102 + 102 + 330 + 330 + 5 + 5) / 6 = 145.666667 國家Israel的平均通話時長是 (33 + 4 + 13 + 13 + 3 + 1 + 1 + 7) / 8 = 9.37500 國家Morocco的平均通話時長是 (33 + 4 + 59 + 59 + 3 + 7) / 6 = 27.5000 全球平均通話時長 = (2 * (33 + 4 + 59 + 102 + 330 + 5 + 13 + 3 + 1 + 7)) / 20 = 55.70000 所以, Peru是唯一的平均通話時長大于全球平均通話時長的國家, 也是唯一的推薦投資的國家.解答:
with tmp as ( # 臨時表tmpselect caller_id caller, duration from Callsunion all # 自聯合select callee_id caller, duration from Calls ) select c.name country from a left join Person p on a.caller=p.id left join Country c on left(p.phone_number, 3)=c.country_code group by c.name having avg(a.duration) > (select avg(duration) from a) # 判斷條件 ;116、1511.消費者下單頻率
難度:★★☆☆☆
表: Customers
+---------------+---------+ | Column Name | Type | +---------------+---------+ | customer_id | int | | name | varchar | | country | varchar | +---------------+---------+ customer_id 是該表主鍵. 該表包含公司消費者的信息.表: Product
+---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | description | varchar | | price | int | +---------------+---------+ product_id 是該表主鍵. 該表包含公司產品的信息. price 是本產品的花銷.表: Orders
+---------------+---------+ | Column Name | Type | +---------------+---------+ | order_id | int | | customer_id | int | | product_id | int | | order_date | date | | quantity | int | +---------------+---------+ order_id 是該表主鍵. 該表包含消費者下單的信息. customer_id 是買了數量為"quantity", id為"product_id"產品的消費者的 id. Order_date 是訂單發貨的日期, 格式為('YYYY-MM-DD').寫一個 SQL 查詢,報告在 2020 年 6 月和 7 月 每個月至少花費 $100 的客戶的 customer_id 和 customer_name 。
以任意順序返回結果表.
查詢結果格式如下例所示
示例 1:
輸入: Customers table: +--------------+-----------+-------------+ | customer_id | name | country | +--------------+-----------+-------------+ | 1 | Winston | USA | | 2 | Jonathan | Peru | | 3 | Moustafa | Egypt | +--------------+-----------+-------------+Product table: +--------------+-------------+-------------+ | product_id | description | price | +--------------+-------------+-------------+ | 10 | LC Phone | 300 | | 20 | LC T-Shirt | 10 | | 30 | LC Book | 45 | | 40 | LC Keychain | 2 | +--------------+-------------+-------------+Orders table: +--------------+-------------+-------------+-------------+-----------+ | order_id | customer_id | product_id | order_date | quantity | +--------------+-------------+-------------+-------------+-----------+ | 1 | 1 | 10 | 2020-06-10 | 1 | | 2 | 1 | 20 | 2020-07-01 | 1 | | 3 | 1 | 30 | 2020-07-08 | 2 | | 4 | 2 | 10 | 2020-06-15 | 2 | | 5 | 2 | 40 | 2020-07-01 | 10 | | 6 | 3 | 20 | 2020-06-24 | 2 | | 7 | 3 | 30 | 2020-06-25 | 2 | | 9 | 3 | 30 | 2020-05-08 | 3 | +--------------+-------------+-------------+-------------+-----------+輸出: +--------------+------------+ | customer_id | name | +--------------+------------+ | 1 | Winston | +--------------+------------+ 解釋: Winston 在2020年6月花費了$300(300 * 1), 在7月花費了$100(10 * 1 + 45 * 2). Jonathan 在2020年6月花費了$600(300 * 2), 在7月花費了$20(2 * 10). Moustafa 在2020年6月花費了$110 (10 * 2 + 45 * 2), 在7月花費了$0.解答:
# Write your MySQL query statement below with tmp as( # 臨時表一 selecto.customer_id,date_format(o.order_date,'%Y-%m') as dt,c.name,o.quantity*p.price as p fromOrders o left joinProduct p on o.product_id=p.product_id left joinCustomers c ono.customer_id=c.customer_id # 三表相連,取其需要的字段 where date_format(o.order_date,'%Y-%m')='2020-06' # 日期條件 ordate_format(o.order_date,'%Y-%m')='2020-07'), # 日期條件 tmp1 as( # 臨時表二 selectcustomer_id,name,dt,sum(p) p # 總價格 from tmp group bycustomer_id,dt) # 分組求總價格select customer_id,name fromtmp1 group by customer_id havingcount(*)>=2 andsum(if(p>=100,0,1))=0 # 條件判斷 ;117、1517.查找擁有有效郵箱的用戶
難度:★★☆☆☆
用戶表: Users
+---------------+---------+ | Column Name | Type | +---------------+---------+ | user_id | int | | name | varchar | | mail | varchar | +---------------+---------+ user_id (用戶 ID)是該表的主鍵。 這個表包含用戶在某網站上注冊的信息。有些郵箱是無效的。寫一條 SQL 語句,查詢擁有有效郵箱的用戶。
有效的郵箱包含符合下列條件的前綴名和域名:
前綴名是包含字母(大寫或小寫)、數字、下劃線 ‘_’、句點 ‘.’ 和/或橫杠 ‘-’ 的字符串。前綴名必須以字母開頭。
域名是 ‘@leetcode.com’ 。
按任意順序返回結果表。
查詢格式如下所示:
Users +---------+-----------+-------------------------+ | user_id | name | mail | +---------+-----------+-------------------------+ | 1 | Winston | winston@leetcode.com | | 2 | Jonathan | jonathanisgreat | | 3 | Annabelle | bella-@leetcode.com | | 4 | Sally | sally.come@leetcode.com | | 5 | Marwan | quarz#2020@leetcode.com | | 6 | David | david69@gmail.com | | 7 | Shapiro | .shapo@leetcode.com | +---------+-----------+-------------------------+結果表: +---------+-----------+-------------------------+ | user_id | name | mail | +---------+-----------+-------------------------+ | 1 | Winston | winston@leetcode.com | | 3 | Annabelle | bella-@leetcode.com | | 4 | Sally | sally.come@leetcode.com | +---------+-----------+-------------------------+ 2 號用戶的郵箱沒有域名。 5 號用戶的郵箱包含非法字符 #。 6 號用戶的郵箱的域名不是 leetcode。 7 號用戶的郵箱以句點(.)開頭。解答:
select* from Users wheremail regexp '^[a-zA-Z][a-zA-Z0-9\\_\\.\\-]*@leetcode\\.com$' # 正則表達式,\\進行轉移 ;118、1527.患某種疾病的患者
難度:★★☆☆☆
患者信息表: Patients
+--------------+---------+ | Column Name | Type | +--------------+---------+ | patient_id | int | | patient_name | varchar | | conditions | varchar | +--------------+---------+ patient_id (患者 ID)是該表的主鍵。 'conditions' (疾病)包含 0 個或以上的疾病代碼,以空格分隔。 這個表包含醫院中患者的信息。寫一條 SQL 語句,查詢患有 I 類糖尿病的患者 ID (patient_id)、患者姓名(patient_name)以及其患有的所有疾病代碼(conditions)。I 類糖尿病的代碼總是包含前綴 DIAB1 。
按任意順序返回結果表。
查詢結果格式如下示例所示:
Patients +------------+--------------+--------------+ | patient_id | patient_name | conditions | +------------+--------------+--------------+ | 1 | Daniel | YFEV COUGH | | 2 | Alice | | | 3 | Bob | DIAB100 MYOP | | 4 | George | ACNE DIAB100 | | 5 | Alain | DIAB201 | +------------+--------------+--------------+結果表: +------------+--------------+--------------+ | patient_id | patient_name | conditions | +------------+--------------+--------------+ | 3 | Bob | DIAB100 MYOP | | 4 | George | ACNE DIAB100 | +------------+--------------+--------------+ Bob 和 George 都患有代碼以 DIAB1 開頭的疾病。解答:
select* frompatients whereconditions regexp '^DIAB1| DIAB1' # 正則表達式,|或者的意思 ;119、1532.最近的三筆訂單
難度:★★★☆☆
表:Customers
+---------------+---------+ | Column Name | Type | +---------------+---------+ | customer_id | int | | name | varchar | +---------------+---------+ customer_id 是該表主鍵 該表包含消費者的信息表:Orders
+---------------+---------+ | Column Name | Type | +---------------+---------+ | order_id | int | | order_date | date | | customer_id | int | | cost | int | +---------------+---------+ order_id 是該表主鍵 該表包含id為customer_id的消費者的訂單信息 每一個消費者 每天一筆訂單寫一個 SQL 語句,找到每個用戶的最近三筆訂單。如果用戶的訂單少于 3 筆,則返回他的全部訂單。
返回的結果按照 customer_name 升序排列。如果排名有相同,則繼續按照 customer_id 升序排列。如果排名還有相同,則繼續按照 order_date 降序排列。
查詢結果格式如下例所示:
Customers +-------------+-----------+ | customer_id | name | +-------------+-----------+ | 1 | Winston | | 2 | Jonathan | | 3 | Annabelle | | 4 | Marwan | | 5 | Khaled | +-------------+-----------+Orders +----------+------------+-------------+------+ | order_id | order_date | customer_id | cost | +----------+------------+-------------+------+ | 1 | 2020-07-31 | 1 | 30 | | 2 | 2020-07-30 | 2 | 40 | | 3 | 2020-07-31 | 3 | 70 | | 4 | 2020-07-29 | 4 | 100 | | 5 | 2020-06-10 | 1 | 1010 | | 6 | 2020-08-01 | 2 | 102 | | 7 | 2020-08-01 | 3 | 111 | | 8 | 2020-08-03 | 1 | 99 | | 9 | 2020-08-07 | 2 | 32 | | 10 | 2020-07-15 | 1 | 2 | +----------+------------+-------------+------+Result table: +---------------+-------------+----------+------------+ | customer_name | customer_id | order_id | order_date | +---------------+-------------+----------+------------+ | Annabelle | 3 | 7 | 2020-08-01 | | Annabelle | 3 | 3 | 2020-07-31 | | Jonathan | 2 | 9 | 2020-08-07 | | Jonathan | 2 | 6 | 2020-08-01 | | Jonathan | 2 | 2 | 2020-07-30 | | Marwan | 4 | 4 | 2020-07-29 | | Winston | 1 | 8 | 2020-08-03 | | Winston | 1 | 1 | 2020-07-31 | | Winston | 1 | 10 | 2020-07-15 | +---------------+-------------+----------+------------+ Winston 有 4 筆訂單, 排除了 "2020-06-10" 的訂單, 因為它是最老的訂單。 Annabelle 只有 2 筆訂單, 全部返回。 Jonathan 恰好有 3 筆訂單。 Marwan 只有 1 筆訂單。 結果表我們按照 customer_name 升序排列,customer_id 升序排列,order_date 降序排列。解答:
# Write your MySQL query statement below with tmp as( # 臨時表 selectc.name,o.customer_id,o.order_id,o.order_date,row_number() over(partition by o.customer_id order by o.order_date desc) rk #排序 fromorders o left joincustomers c # 兩表聯合 ono.customer_id=c.customer_id # 連接條件 ) select name customer_name,customer_id,order_id,order_date fromtmp whererk<4 # 條件,前三筆訂單 order byname,customer_id,order_date desc # 排序條件120、1543.產品名稱格式修復
難度:★★☆☆☆
表:Sales
+--------------+---------+ | Column Name | Type | +--------------+---------+ | sale_id | int | | product_name | varchar | | sale_date | date | +--------------+---------+ sale_id 是該表主鍵 該表的每一行包含了產品的名稱及其銷售日期因為在 2000 年該表是手工填寫的,product_name 可能包含前后空格,而且包含大小寫。
寫一個 SQL 語句報告每個月的銷售情況:
- product_name 是小寫字母且不包含前后空格
- sale_date 格式為 ('YYYY-MM')
- total 是產品在本月銷售的次數
返回結果以 product_name 升序 排列,如果有排名相同,再以 sale_date 升序 排列。
查詢結果格式如下所示:
Sales 表: +------------+------------------+--------------+ | sale_id | product_name | sale_date | +------------+------------------+--------------+ | 1 | LCPHONE | 2000-01-16 | | 2 | LCPhone | 2000-01-17 | | 3 | LcPhOnE | 2000-02-18 | | 4 | LCKeyCHAiN | 2000-02-19 | | 5 | LCKeyChain | 2000-02-28 | | 6 | Matryoshka | 2000-03-31 | +------------+------------------+--------------+Result 表: +--------------+--------------+----------+ | product_name | sale_date | total | +--------------+--------------+----------+ | lcphone | 2000-01 | 2 | | lckeychain | 2000-02 | 2 | | lcphone | 2000-02 | 1 | | matryoshka | 2000-03 | 1 | +--------------+--------------+----------+1 月份,賣了 2 個 LcPhones,請注意產品名稱是小寫的,中間可能包含空格 2 月份,賣了 2 個 LCKeychains 和 1 個 LCPhone 3 月份,賣了 1 個 matryoshka解答:
# Write your MySQL query statement below with tmp as( # 臨時表tmp selectlower(trim(product_name)) product_name, # product_name去掉空格并且全部為小寫date_format(sale_date,'%Y-%m') sale_date # 日期進行轉換 from sales ) selectproduct_name,sale_date,count(*) total # 聚合函數求數量 from tmp group byproduct_name,sale_date # 根據product_name,sale_date進行分組 order byproduct_name,sale_date # 根據product_name,sale_date進行排序 ;擴展:
# 1、字符串去空格 # ①去掉左空格:ltrim(string) # ②去掉右空格:rtrim(string) # ③去掉左右空格:trim(string)# 2、字母大小寫 # ①字母小寫:lower(string) # ②字母大寫:upper(string)總結
以上是生活随笔為你收集整理的LeetCode-SQL(六)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html5 本地文件系统,本地文件系统
- 下一篇: mysql 查询分析工具下载_SQL分析