又来交作业了
627-swap-salary
https://leetcode.com/problems/swap-salary/description/
IF或者CASE
-- if和case when then else end都行
update Salary set sex = if(sex='f', 'm', 'f')
update Salary set sex = case when sex='f' then 'm' else 'f' end
1045-customers-who-bought-all-products
https://leetcode.com/problems/customers-who-bought-all-products/description/
分组之后的COUNT
-- 顾客分组之后的COUNT等于全部商品的Count
select customer_id from Customer
group by customer_id
having count(distinct (product_key)) = (select count(product_key) from Product)
1050-actors-and-directors-who-cooperated-at-least-three-times
https://leetcode.com/problems/actors-and-directors-who-cooperated-at-least-three-times/
合作过至少三次的演员和导演
-- 我还想的是用concat或者自连接呢
select actor_id, director_id
from actordirector
group by actor_id, director_id
having count(*) >= 3
1068-product-sales-analysis-i
https://leetcode.com/problems/product-sales-analysis-i/description/
-- 就这么简单?不敢相信。自己也不太熟练这里
select product_name, year, price
from Product, Sales
where Sales.product_id = Product.product_id
1070-product-sales-analysis-iii
https://leetcode.com/problems/product-sales-analysis-iii/description/
with id_first as (select product_id, min(year) as first_yearfrom Salesgroup by product_id
)select s.product_id, id_first.first_year, s.quantity, s.price
from sales s
join id_first on s.product_id = id_first.product_id and s.year = id_first.first_year;
--
WITH new AS (SELECT product_id, year, RANK() OVER (PARTITION BY product_id ORDER BY year) as rn,quantity,priceFROM Sales)
SELECT product_id, year AS first_year, quantity, price
FROM new
WHERE rn=1;