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

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

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

技术栈 – WhiteNight's Site

更新:MySQL例题:业务查询 – WhiteNight's Site 。大致写了下思路。

USE mydata;
#请在此处添加实现代码
########## Begin ##########
SELECT A.date,IFNULL(ROUND(COUNT(DISTINCT E.user_id)/COUNT(DISTINCT C.user_id),3),0) AS p
FROM login AS A
LEFT JOIN(
SELECT *
FROM login AS B  
WHERE B.date=(
    SELECT MIN(D.date)
    FROM login AS D 
    WHERE B.user_id=D.user_id
    GROUP BY D.user_id
    LIMIT 1
)) AS C
ON A.date=C.date AND A.user_id=C.user_id
LEFT JOIN login AS E 
ON A.user_id=E.user_id AND DATE_ADD(A.date,INTERVAL 1 DAY)=E.date
GROUP BY A.date
ORDER BY A.date ASC
########## End ##########

 二

USE mydata;
#请在此处添加实现代码
########## Begin ##########
SELECT t.user_id,MIN(t.date) AS first_buy_date,MAX(t.date) AS second_buy_date,MAX(t.cnt) AS cnt
FROM(
    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
    FROM order_info AS B 
    WHERE B.status!="no_completed"
    AND B.date>'2021-10-15'
    AND IF(B.product_name="C++" OR B.product_name="JAVA" OR B.product_name="Python",1,0)=1
)t
WHERE t.rk<=2
GROUP BY t.user_id
HAVING COUNT(t.user_id)>=2
########## End ##########

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值