美文网首页
2021-04-04 SQL刷题

2021-04-04 SQL刷题

作者: 杨昊_6c65 | 来源:发表于2021-04-04 19:37 被阅读0次

小红书-计算好评率

select user_name,
sum(case when sat_name = '好评' then 1 else 0 end)/count(sat_name)+'%' as 好评率,
sum(case when sat_name = '中评' then 1 else 0 end)/count(sat_name)+'%' as 中评率,
sum(case when sat_name = '差评' then 1 else 0 end)/count(sat_name)+'%' as 差评率
from
(select *,DATE_FORMAT(sub_time,'%Y-%m-%d') as subtime from redbk_userjudge
where sub_time between '2019-03-01' and '2019-03-31' and user_name = '小张')t
image.png

第二题

select 
user_id,item_id,
case when 是否购买 = 1 and 是否收藏 =1 then 1 else 0 end as "购买且收藏",
case when 是否购买 = 0 and 是否收藏 =1 then 1 else 0 end as "收藏未购买",
case when 是否购买 = 1 and 是否收藏 =0 then 1 else 0 end as "购买未收藏",
case when 是否购买 = 0 and 是否收藏 =0 then 1 else 0 end as "未购买收藏"
FROM
    (select * FROM
        (select user2 as user_id,item2 as item_id,
        (case when item1 is null then 0  else 1 end) as '是否购买',
        (case when item2 is null then 0  else 1 end) as '是否收藏'
    from
                (select a.user_id as user1,a.item_id as item1,b.user_id as user2,b.item_id as item2
                from redbk_orders a right join redbk_favorites b 
                on a.user_id = b.user_id and a.item_id = b.item_id)t1
    )t2
    UNION
    (select user1 as uesr_id,item1 as item_id,
        (case when item1 is null then 0  else 1 end) as '是否购买',
        (case when item2 is null then 0  else 1 end) as '是否收藏'
     from
                (select a.user_id as user1,a.item_id as item1,b.user_id as user2,b.item_id as item2
                from redbk_orders a left join redbk_favorites b 
                on a.user_id = b.user_id and a.item_id = b.item_id)t3)
    )t4
order by user_id,item_id
image.png

笔记一下


image.png

相关文章

  • 2021-04-04 SQL刷题

    小红书-计算好评率 第二题 笔记一下

  • sql刷题

    262.Trips and Users 难题 case when 记得加end, between如果不是数字的话记...

  • SQL 刷题

    https://www.nowcoder.com/ta/sqlhttps://leetcode-cn.com/pr...

  • 刷题sql

    t 175. Combine Two Tables t 176. Second Highest Salary t ...

  • sql 刷题笔记1

    1. 查找重复的邮箱 这道题本质上是一道查找重复数据的题目,常用的思路就是 使用 group by 分组计数,然后...

  • sql刷题笔记(五)

    题目选自leetcode 上的题库 可能不一定都是最优解,答案仅供参考每道题后面都应相应的难度等级,如果没时间做的...

  • sql刷题笔记(七)

    题目选自leetcode 上的题库 可能不一定都是最优解,答案仅供参考每道题后面都应相应的难度等级,如果没时间做的...

  • sql刷题笔记(一)

    题目选自leetcode 上的题库 可能不一定都是最优解,答案仅供参考每道题后面都应相应的难度等级,如果没时间做的...

  • sql刷题笔记(四)

    题目选自leetcode 上的题库 可能不一定都是最优解,答案仅供参考每道题后面都应相应的难度等级,如果没时间做的...

  • sql刷题笔记(六)

    题目选自leetcode 上的题库 可能不一定都是最优解,答案仅供参考每道题后面都应相应的难度等级,如果没时间做的...

网友评论

      本文标题:2021-04-04 SQL刷题

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