简体   繁体   English

SQL累计总和

[英]SQL Cumulative Sum over Time

I am working with SQL Server and Management Studio 2014 and am looking to calculate cumulative dosing of a drug over time for each case in a database. 我正在使用SQL Server和Management Studio 2014,并希望计算数据库中每种情况随时间推移的累积药物剂量。 A hypothetical sample of cases and times follows. 假设的情况和时间样本如下。

Case_Name    | InductionTime           | DoseTime                | EndTime                 |NEdose
2Z378J01     | 2007-03-07 08:58:00.000 | 2007-03-07 09:35:00.000 | 2007-07-03 13:06:15.000 | 0.01
2Z378J01     | 2007-03-07 08:58:00.000 | 2007-03-07 12:04:00.000 | 2007-07-03 13:06:15.000 | 0.02
2Z378J01     | 2007-03-07 08:58:00.000 | 2007-03-07 12:53:00.000 | 2007-07-03 13:06:15.000 | 0
2Z668I02     | 2007-06-06 08:46:00.000 | 2007-06-06 13:22:00.000 | 2007-06-06 13:51:45.000 | 0.04
2Z9C8E01     | 2007-09-12 08:46:00.000 | 2007-09-12 11:22:00.000 | 2007-12-09 11:46:00.000 | 0.02
2Z9C8E01     | 2007-09-12 08:46:00.000 | 2007-09-12 11:30:00.000 | 2007-12-09 11:46:00.000 | 0
2Z9C8E01     | 2007-09-12 08:46:00.000 | 2007-09-12 11:42:00.000 | 2007-12-09 11:46:00.000 | 0.01
2Z9C8E01     | 2007-09-12 08:46:00.000 | 2007-09-12 11:44:00.000 | 2007-12-09 11:46:00.000 | 0.02

What I need is to find the sum of the following: 我需要找到以下各项的总和:

[NEdose1] x ([InductionTime] - [DoseTime1]) +      //only if [InductionTime] > [DoseTime1]
[NEdose1] x ([DoseTime2] - [DoseTime1]) +
[NEdose2] x ([DoseTime3] - [DoseTime2]) + ... +
[NEdose(n-1)] x ([DoseTime(n]) - [DoseTime(n-1)])
[NEdose(n)] x ([EndTime] - [DoseTime(n)])  = TotalDose

Where [NEdose1] represents the value of the NEdose column in row 1 when the table is ordered by DoseTime. 

The numbering scheme is the same for DoseTimes continuing to row n

Grouped by each Case_Name. 按每个Case_Name分组。 Some of the cases have only a NEdose and DoseTime, some have up to 20. The NE dose is in dose/minute, so the units of the time subtraction would be in minutes. 有些情况只有NEdose和DoseTime,有些情况最多为20。NE剂量是以剂量/分钟为单位的,因此减去时间的单位将以分钟为单位。

If you have any suggestions on getting me started (I am just a "hobbyist" with SQL), they would be greatly appreciated. 如果您有关于入门的任何建议(我只是SQL的“爱好者”),将不胜感激。

Edit: I have tried to clarify my desired calculation 编辑:我试图澄清我想要的计算

This is too long for a comment. 这个评论太长了。

I don't follow your logic. 我不遵循你的逻辑。 You have expressions like NEdose1 in the description of the calculation, but there is no column with that name. 您在计算的描述中有类似于NEdose1表达式,但是没有该名称的列。

Given the data structure, I would expect dosetime , endtime , and nedose to be the key columns. 在给定数据结构的情况下,我希望dosetimeendtimenedose是关键列。 Assuming that the dose is per day, something like: 假设剂量是每天,例如:

select case_name,
       sum(nedose * (datediff(day, dosetime, endtime) + 1)) as totaldose
from t
group by case_name;

Given that you want a summary per case, this doesn't seem cumulative. 假设您需要每个案例的摘要,这似乎不是累积性的。

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

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