当前位置:网站首页>Accumulate SQL by date
Accumulate SQL by date
2022-07-23 11:41:00 【AA master Zhao】
Sample data :
uid,dt,cnt
guid01,2018-02-28,1
guid01,2018-03-01,2
guid01,2018-03-02,2
guid01,2018-03-04,0
guid01,2018-03-05,0
guid01,2018-03-06,2
guid01,2018-03-07,3
guid02,2018-03-01,4
guid02,2018-03-02,1
guid02,2018-03-03,0
guid02,2018-03-06,2
The result requires : Accumulate the user's daily amount , The effect is as follows :
guid01,2018-02-28,1
guid01,2018-03-01,3
guid01,2018-03-02,5
guid01,2018-03-04,5
guid01,2018-03-05,5
guid01,2018-03-06,7
sql as follows :
SELECT t1.uid
,t1.dt
,t1.cnt
,sum(t2.cnt) (
SELECT uid
,dt
,cnt
,row_number() OVER (
PARTITION BY uid ORDER BY dt
) rn
FROM user_order
) t1
JOIN (
SELECT uid
,dt
,cnt
,row_number() OVER (
PARTITION BY uid ORDER BY dt
) rn
FROM user_order
) t2 ON t1.uid = t2.uid
AND t1.rn >= t2.rn
GROUP BY t1.uid
,t1.dt
,t1.cnt
ORDER BY t1.dt;Or use sum() over() function :
SELECT uid
,dt
,cnt
,sum(cnt) OVER (
PARTITION BY uid ORDER BY dt
) total_cnt
FROM user_order
边栏推荐
猜你喜欢
![[hudi]hudi compilation and simple use of Hudi & spark and Hudi & Flink](/img/6f/e6f5ef79c232d9b27a8334cd8ddaa5.png)
[hudi]hudi compilation and simple use of Hudi & spark and Hudi & Flink

NFT数字藏品系统开发:深圳晚报《美好时光遨游记》数字藏品上线秒磬

NFT digital collection system development, development trend of Digital Collections

命令执行漏洞及防御

sqli-lab第17~22关通关随笔记

window下vmware使用无线网卡nat的方式上网

XML建模

NFT digital collection development /dapp development

Preliminary study on DC-1 shooting range

Custom formula input box
随机推荐
[metric] use Prometheus to monitor flink1.13org.apache.flink.metrics
Usage of some, every, find, FindIndex
数仓4.0笔记——数仓建模
Custom formula input box
last-child不生效问题
NFT数字藏品系统开发:音乐和NFT的结合
Precautions for realizing "real-time data response" on the page
NFT数字藏品开发/DAPP开发
Upload lab level 1-4
CTF web common software installation and environment construction
Yarn容量调度器设置
[pyautogui learning] screen coordinates and mouse scrolling
Bank of Indonesia governor said the country is actively exploring encrypted assets
中间人攻击arp欺骗及与beef-xss联动
Man in the middle attacks ARP Spoofing and its linkage with beef XSS
mysql根据中文字段首字母排序
美联储布拉德:上周就业报告表明美国经济稳健,可以承受更高的利率
渗透测试基础
CTF-web 常用软件安装及环境搭建
数字藏品开发/数字藏品系统开发解决方案