简体   繁体   English

sql中的累积和和累积计数

[英]Cumulative sum and cumulative count in sql

I am trying establish treatment courses for patients, below is sample table I am working on.我正在尝试为患者建立治疗课程,下面是我正在处理的示例表。

PatientID患者 ID SessionDate会议日期 Next_date下一个日期 diff差异 courses培训班 Coursenum课程编号 course_Count course_Count Total Sesion总会话
10000 10000 12/13/2012 2012 年 12 月 13 日 NULL无效的 1.1 1.1 start开始 1 1 1 1 10 10
10000 10000 12/14/2012 2012 年 12 月 14 日 12/13/2012 2012 年 12 月 13 日 1 1 existing现存的 1 1 2 2 10 10
10000 10000 12/14/2012 2012 年 12 月 14 日 12/14/2012 2012 年 12 月 14 日 0 0 existing现存的 1 1 3 3 10 10
10000 10000 12/17/2012 2012 年 12 月 17 日 12/14/2012 2012 年 12 月 14 日 3 3 existing现存的 1 1 4 4 10 10
10000 10000 12/18/2012 2012 年 12 月 18 日 12/17/2012 2012 年 12 月 17 日 1 1 existing现存的 1 1 5 5 10 10
10000 10000 12/19/2012 2012 年 12 月 19 日 12/18/2012 2012 年 12 月 18 日 1 1 existing现存的 1 1 6 6 10 10
10000 10000 12/21/2012 2012 年 12 月 21 日 12/19/2012 2012 年 12 月 19 日 2 2 existing现存的 1 1 7 7 10 10
10000 10000 12/21/2012 2012 年 12 月 21 日 12/21/2012 2012 年 12 月 21 日 0 0 existing现存的 1 1 8 8 10 10
10000 10000 12/22/2012 2012 年 12 月 22 日 12/21/2012 2012 年 12 月 21 日 1 1 existing现存的 1 1 9 9 10 10
10000 10000 12/24/2012 2012 年 12 月 24 日 12/22/2012 2012 年 12 月 22 日 2 2 existing现存的 1 1 10 10 10 10
10000 10000 9/17/2015 2015 年 9 月 17 日 1/25/2013 2013 年 1 月 25 日 965 965 start开始 2 2 1 1 20 20
10000 10000 9/18/2015 2015 年 9 月 18 日 9/17/2015 2015 年 9 月 17 日 1 1 existing现存的 2 2 2 2 20 20
10000 10000 9/21/2015 2015 年 9 月 21 日 9/18/2015 2015 年 9 月 18 日 3 3 existing现存的 2 2 3 3 20 20
10000 10000 9/22/2015 2015 年 9 月 22 日 9/21/2015 2015 年 9 月 21 日 1 1 existing现存的 2 2 4 4 20 20
10000 10000 9/23/2015 2015 年 9 月 23 日 9/22/2015 2015 年 9 月 22 日 1 1 existing现存的 2 2 5 5 20 20
10000 10000 9/25/2015 2015 年 9 月 25 日 9/23/2015 2015 年 9 月 23 日 2 2 existing现存的 2 2 6 6 20 20
10000 10000 9/28/2015 2015 年 9 月 28 日 9/25/2015 2015 年 9 月 25 日 3 3 existing现存的 2 2 7 7 20 20
10000 10000 9/29/2015 2015 年 9 月 29 日 9/28/2015 2015 年 9 月 28 日 1 1 existing现存的 2 2 8 8 20 20
10000 10000 9/30/2015 2015 年 9 月 30 日 9/29/2015 2015 年 9 月 29 日 1 1 existing现存的 2 2 9 9 20 20
10000 10000 10/2/2015 2015 年 10 月 2 日 9/30/2015 2015 年 9 月 30 日 2 2 existing现存的 2 2 10 10 20 20
10000 10000 10/5/2015 2015 年 10 月 5 日 10/2/2015 2015 年 10 月 2 日 3 3 existing现存的 2 2 11 11 20 20
10000 10000 10/6/2015 2015 年 10 月 6 日 10/5/2015 2015 年 10 月 5 日 1 1 existing现存的 2 2 12 12 20 20
10000 10000 10/7/2015 2015 年 10 月 7 日 10/6/2015 2015 年 10 月 6 日 1 1 existing现存的 2 2 13 13 20 20
10000 10000 10/9/2015 2015 年 10 月 9 日 10/7/2015 2015 年 10 月 7 日 2 2 existing现存的 2 2 14 14 20 20
10000 10000 10/12/2015 2015 年 10 月 12 日 10/9/2015 2015 年 10 月 9 日 3 3 existing现存的 2 2 15 15 20 20
10000 10000 10/13/2015 2015 年 10 月 13 日 10/12/2015 2015 年 10 月 12 日 1 1 existing现存的 2 2 16 16 20 20
10000 10000 10/14/2015 2015 年 10 月 14 日 10/13/2015 2015 年 10 月 13 日 1 1 existing现存的 2 2 17 17 20 20
10000 10000 10/16/2015 2015 年 10 月 16 日 10/14/2015 2015 年 10 月 14 日 2 2 existing现存的 2 2 18 18 20 20
10000 10000 10/19/2015 2015 年 10 月 19 日 10/16/2015 2015 年 10 月 16 日 3 3 existing现存的 2 2 19 19 20 20
10000 10000 10/20/2015 2015 年 10 月 20 日 10/19/2015 2015 年 10 月 19 日 1 1 existing现存的 2 2 20 20 20 20

I achieved this output using python我使用python实现了这个输出

Note: 1.1 in diff column is just a placeholeder for NULL注意: diff 列中的 1.1 只是 NULL 的占位符

sessions['Coursenum']=(sessions.status.eq('start')).cumsum() sessions['course_count']=sessions.groupby(['Coursenum']).cumcount()+1 sessions['TotalSessions']=sessions.groupby('Coursenum')['course_count'].transform('max')会话['Coursenum']=(sessions.status.eq('start')).cumsum() 会话['course_count']=sessions.groupby(['Coursenum']).cumcount()+1 个会话['TotalSessions ']=sessions.groupby('Coursenum')['course_count'].transform('max')

I wanted to program this using sql.我想用 sql 来编程。 Is there a way to do this in sql?有没有办法在sql中做到这一点?

I think window functions using count() and sum() would work here with the proper order by.我认为使用 count() 和 sum() 的窗口函数可以按正确的顺序在这里工作。

select count(coursenum) over (partition by coursenum order by sessiondate) should get you cumulative count. select count(coursenum) over (partition by coursenum order by sessiondate)应该让你累积计数。

We can use window functions in SQL whenever we want to show group-level aggregations in raw data itself.每当我们想在原始数据本身中显示组级聚合时,我们都可以在 SQL 中使用窗口函数。

select *, max(course_Count) over (partition by Coursenum order by Session_date) as Total_Session 
from( select *, count(*) over (partition by Coursenum order by Session_date) as course_Count 
from( select *,sum(case when courses = 'start' then 1 else 0 end) over (order by Session_date) as Coursenum
from [table_name]
) A)

Refer this link for more information on window functions and this for Case statement有关窗口函数的更多信息,请参阅此链接,并参阅链接以获取 Case 语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM