sql开窗函数语法糖1

在翻mysql文档看见的开窗函数的语法糖,感觉很有意思,之前都没见过

-- 准备表数据
CREATE TABLE LOGIN_LOG(UID VARCHAR(10),LOG_DATE DATE);
INSERT INTO LOGIN_LOG VALUES(1,'2024-01-01'),
(1,'2024-01-02)'
(1,'2024-01-03'),
(1,'2024-01-04'),
(1,'2024-01-05'),
(1,'2024-01-06'),
(1,'2024-01-07'),
(2,'2024-01-03'),
(3,'2024-01-06'),
(4,'2024-01-02'),
(1,'2024-01-21'),
(8,'2024-01-01'),
(3,'2024-01-23'),
(4,'2024-01-12'),
(5,'2024-01-11'),
(1,'2024-01-22'),
(1,'2024-01-23'),
(5,'2024-01-23'),
(5,'2024-01-24'),
(5,'2024-01-25'),
(5,'2024-01-26'),
(5,'2024-01-27'),
(5,'2024-01-28'),
(5,'2024-01-29')

-- 分组查询 WINDOW win_name AS (PARTITION BY .. ORDER BY ..)
SELECT UID,LOG_DATE,RANK() OVER(WIN_NAME) RO FROM LOGIN_LOG WINDOW WIN_NAME AS (PARTITION BY UID ORDER BY LOG_DATE);
-- 将over部分语句放在表屁股后面形成一个公共表达式,这样有相同window的时候直接用,不需要重复写了
-- 目前测试mysql8/pgsql16/orcle21都能用,需要注意的是这个语法糖不能用在子查询里


-- 过滤器 FILTER (WHERE ...)
SELECT COUNT(UID) FILTER (WHERE UID='1') FROM LOGIN_LOG;
-- 按条件过滤后聚合



-- 查找连续登录用户
SELECT *
FROM (
    SELECT uid,
       log_date,
       lead(ld,5,'2099-12-12') OVER (PARTITION BY uid ORDER BY log_date) lld
    FROM (
        SELECT uid,
               log_date,
               LEAD(log_date, 1, '2099-12-12') OVER (PARTITION BY uid ORDER BY log_date) ld
            FROM tmp.LOGIN_LOG) a
            WHERE ld - log_date = 1) b
WHERE lld - log_date = 6;
-- 连续7天登录用户

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值