力扣MySQL刷題記錄(二)

上一篇:力扣MySQL刷題記錄 點擊閱讀

本次更新的刷題記錄如下
每道題,自己思路一個,吸取評論區思路一個,若和評論區答案差不多一樣,則保留一個

--https://leetcode-cn.com/problems/exchange-seats/
--這個方法我不太懂
select b.id,a.student from seat as a,seat as b,(select count(*) as cnt from seat) as c where b.id=1^(a.id-1)+1 || (c.cnt%2 && b.id=c.cnt && a.id=c.cnt);
--懵逼方法2
select if(id%2=0,id-1,if(id=cnt,id,id+1)) as id,student from (select count(*) as cnt from seat)as a,seat order by id;
--懵逼方法3
select a.id,ifnull(b.student,a.student) as student from seat as a left join seat as b on (a.id%2=1 && a.id=b.id-1) || (a.id%2=0 && a.id=b.id+1) order by a.id;
--改ID的思路解法
select (case
when id%2=0 then id-1
when id=(select max(id) from seat) then id
else id+1 end) as id ,student
from seat order by id;
--雙層if換ID
SELECT
    IF(MOD(id, 2)= 0, id-1, IF(id=(
            SELECT MAX(id) FROM seat        
        ), id, id+1)) AS id,    -- id爲偶數則-1,爲奇數且不是最大值則+1
    student    
FROM seat
ORDER BY id

--https://leetcode-cn.com/problems/customers-who-never-order/
--訂單爲0的客戶
--官方解釋
select name as Customers
from customers  where customers.Id not in (select customerid from orders)

--https://leetcode-cn.com/problems/reformat-department-table/
--相對困難,兩個表合併問題,且把1維度表改爲了2維度表

--方法2
SELECT DISTINCT id AS "id",
SUM(IF (month = "Jan", revenue, null)) AS "Jan_Revenue",
SUM(IF (month = "Feb", revenue, null)) AS "Feb_Revenue",
SUM(IF (month = "Mar", revenue, null)) AS "Mar_Revenue",
SUM(IF (month = "Apr", revenue, null)) AS "Apr_Revenue",
SUM(IF (month = "May", revenue, null)) AS "May_Revenue",
SUM(IF (month = "Jun", revenue, null)) AS "Jun_Revenue",
SUM(IF (month = "Jul", revenue, null)) AS "Jul_Revenue",
SUM(IF (month = "Aug", revenue, null)) AS "Aug_Revenue",
SUM(IF (month = "Sep", revenue, null)) AS "Sep_Revenue",
SUM(IF (month = "Oct", revenue, null)) AS "Oct_Revenue",
SUM(IF (month = "Nov", revenue, null)) AS "Nov_Revenue",
SUM(IF (month = "Dec", revenue, null)) AS "Dec_Revenue" 
FROM Department GROUP BY id

--https://leetcode-cn.com/problems/rank-scores/
--排序三種解說:https://leetcode-cn.com/problems/rank-scores/solution/tu-jie-sqlmian-shi-ti-jing-dian-pai-ming-wen-ti-by/
--一種思路
SELECT 
    a.Score AS Score,
    COUNT(DISTINCT b.Score) AS Rank -- 注意按題目要求去重複值
FROM Scores AS a, Scores AS b
WHERE b.Score >= a.Score    -- 表b中有x個非重複值大於等於表a當前值,則表a當前成績排名爲x
GROUP BY a.id   -- 由於成績即使重複也要顯示,故通過id分組
ORDER BY a.Score DESC

--https://leetcode-cn.com/problems/department-highest-salary/
--我自己只能寫到這個層次目前,但並沒有解決問題
select d.name as Department , e.name  as Employee, e.salary 
from employee as e left join department as d
on e.departmentId = d.Id 
group by d.name
--官方解釋 表子查詢 + 內連接
SELECT
    Department.name AS 'Department',
    Employee.name AS 'Employee',
    Salary
FROM
    Employee
        JOIN
    Department ON Employee.DepartmentId = Department.Id
WHERE
    (Employee.DepartmentId , Salary) IN
    (   SELECT
            DepartmentId, MAX(Salary)
        FROM
            Employee
        GROUP BY DepartmentId
	)
;


--https://leetcode-cn.com/problems/department-top-three-salaries/
--參考答案裏有效代碼如下
SELECT
	Department.NAME AS Department,
	e1.NAME AS Employee,
	e1.Salary AS Salary 
FROM
	Employee AS e1,Department 
WHERE
	e1.DepartmentId = Department.Id 
	AND 3 > (SELECT  count( DISTINCT e2.Salary ) 
			 FROM	Employee AS e2 
			 WHERE	e1.Salary < e2.Salary 	AND e1.DepartmentId = e2.DepartmentId 	) 
ORDER BY Department.NAME,Salary DESC;

--三個連續日期大於100人流量  https://leetcode-cn.com/problems/human-traffic-of-stadium/
--一個解析所云:https://leetcode-cn.com/problems/human-traffic-of-stadium/solution/ti-yu-guan-de-ren-liu-liang-by-little_bird/
SELECT distinct a.*
FROM stadium as a,stadium as b,stadium as c
where ((a.id = b.id-1 and b.id+1 = c.id) or
       (a.id-1 = b.id and a.id+1 = c.id) or
       (a.id-1 = c.id and c.id-1 = b.id))
  and (a.people>=100 and b.people>=100 and c.people>=100)
order by a.id;
--另一種解法:
select
	id, to_char(visit_date, 'yyyy-mm-dd') as visit_date, people
from
	(select
		id, visit_date, people,
		count(1) over (partition by offset) cnt
	from
		(select
			id, visit_date, people,
			(row_number() over (order by id) - id) offset
		from stadium
		where people >= 100
		)
	)
where cnt >= 3   -- 連續 3 天(及以上)
order by id

--第三種解法
SELECT DISTINCT S1.*
FROM stadium AS S1,stadium AS S2,stadium AS S3
WHERE S1.people>=100 AND S2.people>=100 AND S3.people>=100 AND (
	S1.id +1 = S2.id AND S1.id+2=S3.id OR
	S1.id +1 = S2.id AND S1.id-1=S3.id OR
	S1.id -1 = S2.id AND S1.id-2=S3.id
)
ORDER BY S1.id

--
--解法1
SELECT T.request_at AS `Day`, 
	ROUND(
			SUM(
				IF(T.STATUS = 'completed',0,1)
			)
			/ 
			COUNT(T.STATUS),
			2
	) AS `Cancellation Rate`
FROM Trips AS T
JOIN Users AS U1 ON (T.client_id = U1.users_id AND U1.banned ='No')
JOIN Users AS U2 ON (T.driver_id = U2.users_id AND U2.banned ='No')
WHERE T.request_at BETWEEN '2013-10-01' AND '2013-10-03'
GROUP BY T.request_at
--解法2
SELECT t.Request_at AS `Day`,ROUND(SUM(CASE t.`Status` WHEN 'cancelled_by_client' THEN 1 WHEN 'cancelled_by_driver' THEN 1 ELSE 0 END)/COUNT(t.`Status`) ,2)AS `Cancellation Rate`
FROM Trips t
JOIN Users d ON t.Client_Id = d.Users_Id AND d.Banned = 'NO'
JOIN Users c ON t.Driver_Id = c.Users_Id AND c.Banned =  'NO'
WHERE t.Request_at BETWEEN '2013-10-01' AND '2013-10-03'
GROUP BY t.Request_at
發佈了44 篇原創文章 · 獲贊 5 · 訪問量 1945
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章