• 实验11 SQL互联网业务查询-2


    这就是SQL题带给我的自信😕

    可能发题解到博客上,主要是写完一遍实在不想看第二遍,太长了,优化都不想优化,看着头疼。

    技术栈 – WhiteNight's Site

    1. USE mydata;
    2. #请在此处添加实现代码
    3. ########## Begin ##########
    4. SELECT A.date,IFNULL(ROUND(COUNT(DISTINCT E.user_id)/COUNT(DISTINCT C.user_id),3),0) AS p
    5. FROM login AS A
    6. LEFT JOIN(
    7. SELECT *
    8. FROM login AS B
    9. WHERE B.date=(
    10. SELECT MIN(D.date)
    11. FROM login AS D
    12. WHERE B.user_id=D.user_id
    13. GROUP BY D.user_id
    14. LIMIT 1
    15. )) AS C
    16. ON A.date=C.date AND A.user_id=C.user_id
    17. LEFT JOIN login AS E
    18. ON A.user_id=E.user_id AND DATE_ADD(A.date,INTERVAL 1 DAY)=E.date
    19. GROUP BY A.date
    20. ORDER BY A.date ASC
    21. ########## End ##########

     二

    1. USE mydata;
    2. #请在此处添加实现代码
    3. ########## Begin ##########
    4. SELECT t.user_id,MIN(t.date) AS first_buy_date,MAX(t.date) AS second_buy_date,MAX(t.cnt) AS cnt
    5. FROM(
    6. SELECT *,COUNT(B.user_id)over(partition by B.user_id) AS cnt,row_number()over(partition by B.user_id order by B.date ASC) AS rk
    7. FROM order_info AS B
    8. WHERE B.status!="no_completed"
    9. AND B.date>'2021-10-15'
    10. AND IF(B.product_name="C++" OR B.product_name="JAVA" OR B.product_name="Python",1,0)=1
    11. )t
    12. WHERE t.rk<=2
    13. GROUP BY t.user_id
    14. HAVING COUNT(t.user_id)>=2
    15. ########## End ##########

    1. USE mydata;
    2. #请在此处添加实现代码
    3. ########## Begin ##########
    4. SELECT t2.product_name,t2.user_id,t2.rnk,CONCAT(ROUND(t2.incomp_rate,2),'%') AS incomp_rate
    5. FROM(
    6. SELECT *,dense_rank()over(partition by t.product_name order by t.incomp_rate DESC) AS rnk
    7. FROM (
    8. SELECT A.user_id,A.product_name,ROUND(COUNT(IF(A.status='no_completed',1,NULL))*100/COUNT(A.status),4) AS incomp_rate
    9. FROM order_info AS A
    10. WHERE A.date>='2021-10-16' AND A.date<='2021-10-31'
    11. AND EXISTS(
    12. SELECT 1
    13. FROM order_info AS B
    14. WHERE B.user_id=A.user_id AND B.product_name=A.product_name
    15. AND B.date>='2021-10-16' AND B.date<='2021-10-31'
    16. AND B.status='no_completed'
    17. )
    18. GROUP BY A.user_id,A.product_name
    19. )t
    20. )AS t2
    21. WHERE t2.rnk<=3
    22. ORDER BY t2.product_name ASC,t2.rnk ASC
    23. ########## End ##########

  • 相关阅读:
    Ubuntu磁盘满了,导致黑屏
    vite-plugin-html 使用方法文档记录
    权限 chmod
    Vue(第十七课)AXIOS对JSON数据的增删改查
    工业环网交换机运行原理
    matlab 分数阶混沌系统的完全同步控制
    [山东科技大学OJ]1176 Problem E: 数组去重
    PHP实现输入一年里的第N周求第N周的日期范围
    c++-继承详解
    基于微信小程序云开(统计学生信息并导出excel)2.0版
  • 原文地址:https://blog.csdn.net/white_night_SZTU/article/details/134448691