美文网首页
Leetcode 1127. 用户购买平台

Leetcode 1127. 用户购买平台

作者: 七齐起器 | 来源:发表于2021-11-25 16:49 被阅读0次

https://leetcode-cn.com/problems/user-purchase-platform/

select 
c.spend_date
,c.platform
,sum(total_amount) total_amount
,sum(total_users) total_users
from(
    select
    b.spend_date      spend_date
    ,b.platform       platform
    ,sum(b.total_amount)   total_amount
    ,count(b.user_id) total_users
    from
    (
        select
        a.spend_date
        ,a.user_id
        ,case 
            when ifnull(a.mobile,0)>0 and ifnull(a.desktop,0)>0 then 'both'
            when ifnull(a.mobile,0)>0 and ifnull(a.desktop,0)=0 then 'mobile'
            when ifnull(a.mobile,0)=0 and ifnull(a.desktop,0)>0 then 'desktop'
        end platform
        ,ifnull(a.mobile,0)+ifnull(a.desktop,0) total_amount
        from
        (
            select 
            spend_date
            ,user_id
            ,MAX(CASE WHEN platform='mobile' then amount  else 0 end ) mobile 
            ,MAX(CASE WHEN platform='desktop' then amount else 0 end ) desktop
            from Spending
            group by spend_date,user_id
        )a  group by a.spend_date,a.user_id
    )b group by b.spend_date,b.platform 
    union
    select distinct spend_date,'both',0,0 from Spending
    union
    select distinct spend_date,'mobile',0,0 from Spending
    union
    select distinct spend_date,'desktop',0,0 from Spending
)c group by c.spend_date,c.platform
   order by c.total_amount,c.total_users

相关文章

网友评论

      本文标题:Leetcode 1127. 用户购买平台

      本文链接:https://www.haomeiwen.com/subject/vbfztrtx.html