题解 | 谁能告诉我为什么不对!

店铺901国庆期间的7日动销率和滞销率

https://www.nowcoder.com/practice/e7837f66e8fb4b45b694d24ea61f0dc9

with

    sale_product as (

        select

            b.uid,

            b.order_id,

            b.event_time,

            c.product_id,

            a.shop_id

        from

            tb_order_overall b

            left join tb_order_detail c on b.order_id = c.order_id

            left join tb_product_info a on c.product_id = a.product_id

    )

select

date_format(dt,'%Y-%m-%d') dt,

sale_rate,

unsale_rate

from

(

select

    dt,

    round(sale_product_cnt / all_product_cnt, 3) as sale_rate,

    round(

        (all_product_cnt - sale_product_cnt) / all_product_cnt,

        3

    ) as unsale_rate

from

    (

        select

            '2021-10-02' dt,

            shop_id,

            count(product_id) as all_product_cnt

        from

            tb_product_info a

        where

            date (a.release_time) <= '2021-10-02'

            and shop_id = 901

        group by 1,2

    ) t1

    left join (

        select

            shop_id,

            count(distinct product_id) as sale_product_cnt

        from

            sale_product b

        where

            date (event_time) between '2021-09-26' and '2021-10-02'

            and shop_id = 901

            group by 1

    ) t2 on t1.shop_id = t2.shop_id

    union all

    select

    dt,

    round(sale_product_cnt / all_product_cnt, 3) as sale_rate,

    round(

        (all_product_cnt - sale_product_cnt) / all_product_cnt,

        3

    ) as unsale_rate

from

    (

        select

            '2021-10-03' dt,

            shop_id,

            count(product_id) as all_product_cnt

        from

            tb_product_info a

        where

            date (a.release_time) <= '2021-10-03'

            and shop_id = 901

        group by 1,2

    ) t1

    left join (

        select

            shop_id,

            count(distinct product_id) as sale_product_cnt

        from

            sale_product b

        where

            date (event_time) between '2021-09-27' and '2021-10-03'

            and shop_id = 901

            group by 1

    ) t2 on t1.shop_id = t2.shop_id

union all

select

    dt,

    round(sale_product_cnt / all_product_cnt, 3) as sale_rate,

    round(

        (all_product_cnt - sale_product_cnt) / all_product_cnt,

        3

    ) as unsale_rate

from

    (

        select

            '2021-10-01' dt,

            shop_id,

            count(product_id) as all_product_cnt

        from

            tb_product_info a

        where

            date (a.release_time) <= '2021-10-01'

            and shop_id = 901

        group by 1,2

    ) t1

    left join (

        select

            shop_id,

            count(distinct product_id) as sale_product_cnt

        from

            sale_product b

        where

            date (event_time) between '2021-09-25' and '2021-10-01'

            and shop_id = 901

            group by 1

    ) t2 on t1.shop_id = t2.shop_id

) t

#sql练习日常##悬赏#
全部评论

相关推荐

04-30 21:35
已编辑
长安大学 C++
晓沐咕咕咕:评论区没被女朋友好好对待过的计小将可真多。觉得可惜可以理解,毕竟一线大厂sp。但是骂楼主糊涂的大可不必,说什么会被社会毒打更是丢人。女朋友体制内生活有保障,读研女朋友还供着,都准备订婚了人家两情相悦,二线本地以后两口子日子美滋滋,哪轮到你一个一线城市房子都买不起的996清高计小将在这说人家傻😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务