mysql,力扣,

https://leetcode-cn.com/problemset/database/?difficulty=%E5%9B%B0%E9%9A%BE

mysql,力扣,_第1张图片
mysql,力扣,_第2张图片

with sc as (
    select player_id, sum(score) score from (
    select first_player player_id,sum(first_score) score
    from Matches 
    group by first_player
    union all
    select second_player player_id,sum(second_score) score
    from Matches 
    group by second_player)tmp group by player_id)
 
select  p.group_id,p.player_id player_id from 
players p join sc on p.player_id = sc.player_id
where (p.group_id,sc.score) in (
    select p.group_id,max(score) score from 
    players p join sc on p.player_id = sc.player_id
    group by p.group_id)
group by p.group_id
having min(p.player_id)

mysql,力扣,_第3张图片
mysql,力扣,_第4张图片

# Write your MySQL query statement below
with sc as (
    select distinct spend_date, "desktop" as platform
    from Spending
    union
    select distinct spend_date, "mobile" as platform
    from Spending
    union
    select distinct spend_date, "both" as platform
    from Spending 
        ),
    sp as (
        select *,if ( platform = 'mobile',1,2)flag from Spending
    ) 

select  sc.spend_date,sc.platform,sum(ifnull(tmp.money,0))total_amount,count(tmp.platform) total_users
from 
sc left join (
    select  sp.spend_date , sp.user_id,
    case 
    when sum(sp.flag)=1 then 'mobile'
    when  sum(sp.flag)=2 then 'desktop'
    else 'both' end platform
    , ifnull(sum(sp.amount),0) money 
    from sp
    group by  sp.spend_date , sp.user_id)tmp 
on tmp.spend_date = sc.spend_date and tmp.platform = sc.platform
group by 
 sc.spend_date,sc.platform

mysql,力扣,_第5张图片
mysql,力扣,_第6张图片
mysql,力扣,_第7张图片

# Write your MySQL query statement below
with sc as
    (select order_id  from (
        select  order_id ,ROW_NUMBER() over(partition by seller_id  order by order_date ) rankx
        from Orders ) a where a.rankx=2),        
tmp as
    (select * from Orders where order_id in  (
                select order_id from sc )),
other as 
(select tmp.*  ,items.item_brand from tmp join items on tmp.item_id = items.item_id        
                        )               
select * from (
    select u.user_id seller_id,
    if(u.favorite_brand =other.item_brand,'yes','no' )2nd_item_fav_brand
    from users u  join   other
    on other.seller_id = u.user_id 
    union 
    select user_id seller_id,'no' 2nd_item_fav_brand from Users
    where  user_id not in (
        select u.user_id item_id    
        from users u  join   other
        on other.seller_id = u.user_id 
    ) )ans 
order by ans.seller_id

你可能感兴趣的:(mysql)