当前位置:网站首页>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
边栏推荐
- NFT digital collection system development: Xu Beihong Art Museum unveiled through the digital collection platform
- Precautions for realizing "real-time data response" on the page
- 数仓4.0笔记——用户行为数据采集三
- Dynamically set the theme color of the card
- kubesphere haproxy+keepalived (一)
- NFT数字藏品开发/DAPP开发
- How to customize JSP Tags
- The tree form based on El table and JS xlsx realize the function of downloading excel (II)
- Typescript introduction
- D2dengine edible tutorial (2) -- drawing images
猜你喜欢

NFT数字藏品开发/DAPP开发

MySQL add, delete, modify, query & advanced query statements

my_ Implementation of strcpy (classic, simple, practical, collection)

MySQL functions & views & import and export

xtu-ctf Challenges-Reverse 1、2

动态设置卡片的主题色

Digital collection development / meta universe digital collection development

NepCTF 2022 MISC <签到题>(极限套娃)

Security problems of FileInfo in PHP file upload

NFT数字藏品开发:京东“奇达熊带你游京城”旅游套装
随机推荐
Php+ code cloud code hook automatically updates online code
美联储理事沃勒:去中心化金融最终可能会改变传统金融市场
sqli-lab 1-16通关随笔记
Scala之二流程控制
按照日期进行累加sql
Custom MVC (Part 1)
[deployment] cluster deployment and startup of presto-server-0.261.tar.gz
Fed Brad: last week's employment report showed that the U.S. economy is robust and can withstand higher interest rates
window下vmware使用无线网卡nat的方式上网
NepCTF2022 Writeup
mysql修改函数权限未生效
Bank of Indonesia governor said the country is actively exploring encrypted assets
NFT digital collection system development: Shenzhen Evening News "good times travel" digital collection online seconds chime
NFT数字藏品系统开发,数字藏品的发展趋势
Development of digital collection system / introduction of digital collection scheme
数仓4.0笔记——用户行为数据采集三
Simple implementation of rectangular area block
数字藏品系统开发:NFT的主要特点有哪些?
CTF web common software installation and environment construction
存储过程 两次排序加一次随机取数据