从不同的生产运行中获取时间戳的生产总和

时间:2014-04-10 06:47:59

标签: sql

我目前有一个表格,显示特定productionID及其runId在特定时间计算的项目数(生产可以在不同的机器上运行)。见下文。

根据这些数据,我希望在相同的tmTimestamps中计算整个生产。 如果我只是按生产ID和tmTimestamp分组,我得到正确的值,直到最后一个时间戳,其中我只有一个ProductionRunID的值。

编辑:示例:我们在2014-04-08 22:17:38.000只有一个来自productionRunID 3080的值,这是37501.问题是我们也从其他productionRunID 3079生成了19802,但这行已经停止报告,因为生产运行在那里结束。

有什么建议吗?

我有什么:

+--------------+-----------------+-------------------------+---------+
| ProductionID | ProductionRunID |       tmTimeStamp       | Counted |
+--------------+-----------------+-------------------------+---------+
|       889929 |            3079 | 2014-04-08 20:38:01.000 |       0 |
|       889929 |            3079 | 2014-04-08 20:43:01.000 |       0 |
|       889929 |            3079 | 2014-04-08 20:48:02.000 |    2756 |
|       889929 |            3079 | 2014-04-08 20:53:02.000 |    8746 |
|       889929 |            3079 | 2014-04-08 20:58:03.000 |   15005 |
|       889929 |            3079 | 2014-04-08 21:03:04.000 |   19477 |
|       889929 |            3079 | 2014-04-08 21:04:10.000 |   19802 |
|       889929 |            3079 | 2014-04-08 21:09:11.000 |   19802 |
|       889929 |            3080 | 2014-04-08 21:09:11.000 |       0 |
|       889929 |            3080 | 2014-04-08 21:14:11.000 |       0 |
|       889929 |            3079 | 2014-04-08 21:14:11.000 |   19802 |
|       889929 |            3080 | 2014-04-08 21:19:12.000 |    5216 |
|       889929 |            3079 | 2014-04-08 21:19:12.000 |   19802 |
|       889929 |            3080 | 2014-04-08 21:24:13.000 |   11480 |
|       889929 |            3079 | 2014-04-08 21:24:13.000 |   19802 |
|       889929 |            3079 | 2014-04-08 21:29:14.000 |   19802 |
|       889929 |            3080 | 2014-04-08 21:29:14.000 |   17791 |
|       889929 |            3080 | 2014-04-08 21:34:15.000 |   24065 |
|       889929 |            3079 | 2014-04-08 21:34:15.000 |   19802 |
|       889929 |            3079 | 2014-04-08 21:39:16.000 |   19802 |
|       889929 |            3080 | 2014-04-08 21:39:16.000 |   30356 |
|       889929 |            3080 | 2014-04-08 21:43:03.000 |   34881 |
|       889929 |            3079 | 2014-04-08 21:43:03.000 |   19802 |
|       889929 |            3079 | 2014-04-08 21:48:04.000 |   19802 |
|       889929 |            3080 | 2014-04-08 21:48:04.000 |   37501 |
|       889929 |            3080 | 2014-04-08 21:50:44.000 |   37501 |
|       889929 |            3079 | 2014-04-08 21:50:44.000 |   19802 |
|       889929 |            3079 | 2014-04-08 21:51:54.000 |   19802 |
|       889929 |            3080 | 2014-04-08 21:51:54.000 |   37501 |
|       889929 |            3080 | 2014-04-08 21:56:55.000 |   37501 |
|       889929 |            3079 | 2014-04-08 21:56:55.000 |   19802 |
|       889929 |            3079 | 2014-04-08 22:01:56.000 |   19802 |
|       889929 |            3080 | 2014-04-08 22:01:56.000 |   37501 |
|       889929 |            3079 | 2014-04-08 22:02:37.000 |   19802 |
|       889929 |            3080 | 2014-04-08 22:02:37.000 |   37501 |
|       889929 |            3080 | 2014-04-08 22:07:37.000 |   37501 |
|       889929 |            3080 | 2014-04-08 22:12:37.000 |   37501 |
|       889929 |            3080 | 2014-04-08 22:17:38.000 |   37501 |
|       889929 |            3080 | 2014-04-08 22:22:38.000 |   37501 |
+--------------+-----------------+-------------------------+---------+

我想要的是什么:

+--------------+-------------------------+---------+
| ProductionID |       tmTimeStamp       | Counted |
+--------------+-------------------------+---------+
|       889929 | 2014-04-08 20:17:58.000 |       0 |
|       889929 | 2014-04-08 20:22:59.000 |       0 |
|       889929 | 2014-04-08 20:27:59.000 |       0 |
|       889929 | 2014-04-08 20:33:00.000 |       0 |
|       889929 | 2014-04-08 20:38:01.000 |       0 |
|       889929 | 2014-04-08 20:43:01.000 |       0 |
|       889929 | 2014-04-08 20:48:02.000 |    2756 |
|       889929 | 2014-04-08 20:53:02.000 |    8746 |
|       889929 | 2014-04-08 20:58:03.000 |   15005 |
|       889929 | 2014-04-08 21:03:04.000 |   19477 |
|       889929 | 2014-04-08 21:04:10.000 |   19802 |
|       889929 | 2014-04-08 21:09:11.000 |   19802 |
|       889929 | 2014-04-08 21:14:11.000 |   19802 |
|       889929 | 2014-04-08 21:19:12.000 |   25018 |
|       889929 | 2014-04-08 21:24:13.000 |   31282 |
|       889929 | 2014-04-08 21:29:14.000 |   37593 |
|       889929 | 2014-04-08 21:34:15.000 |   43867 |
|       889929 | 2014-04-08 21:39:16.000 |   50158 |
|       889929 | 2014-04-08 21:43:03.000 |   54683 |
|       889929 | 2014-04-08 21:48:04.000 |   57303 |
|       889929 | 2014-04-08 21:50:44.000 |   57303 |
|       889929 | 2014-04-08 21:51:54.000 |   57303 |
|       889929 | 2014-04-08 21:56:55.000 |   57303 |
|       889929 | 2014-04-08 22:01:56.000 |   57303 |
|       889929 | 2014-04-08 22:02:37.000 |   57303 |
|       889929 | 2014-04-08 22:07:37.000 |   57303 |
|       889929 | 2014-04-08 22:12:37.000 |   57303 |
|       889929 | 2014-04-08 22:17:38.000 |   57303 |
|       889929 | 2014-04-08 22:22:38.000 |   57303 |
|       889929 | 2014-04-08 22:27:39.000 |   57303 |
|       889929 | 2014-04-08 22:32:39.000 |   57303 |
|       889929 | 2014-04-08 22:37:40.000 |   57303 |
|       889929 | 2014-04-08 22:42:41.000 |   57303 |
|       889929 | 2014-04-08 22:47:42.000 |   57303 |
|       889929 | 2014-04-08 22:52:43.000 |   57303 |
|       889929 | 2014-04-08 22:57:44.000 |   57303 |
|       889929 | 2014-04-08 22:59:26.000 |   57303 |
|       889929 | 2014-04-08 23:04:27.000 |   57303 |
|       889929 | 2014-04-08 23:04:47.000 |   57303 |
+--------------+-------------------------+---------+

EDIT2:添加了示例结果。

2 个答案:

答案 0 :(得分:1)

您的问题仍然很难说明,这使得其他人难以提供帮助。我从您的各种评论中收集到您希望结果包含counted列的小计,该列是同一counted的所有按时间顺序排列的行中productionId的总和productionRunId。这个的SQL是:

select t1.ProductionID, t1.ProductionRunID, t1.tmTimeStamp, t1.counted, 
(
  select sum(counted)
  from prod as t2 
  where t1.ProductionID = t2.ProductionID
  and   t1.ProductionRunID = t2.ProductionRunID
  and   t1.tmTimeStamp >= t2.tmTimeStamp
) as sub_total
from prod as t1
order by 1, 2, 3
;

您尚未说明您正在使用哪个数据库,而更复杂的SQL通常会针对特定于实现(或者使用特定数据库的功能更简单)。我假设了mySQL。

这是一个小提琴来说明它:http://sqlfiddle.com/#!2/5f794/7

请注意,在您的原始问题中,您声明您想要group by时间戳 - 这是错误的并且导致读者误解了问题。请在下次尝试更清楚,并举例说明所需的结果和表格结构。

答案 1 :(得分:0)

我认为这就是你要找的东西:

select
    ProductionId
    ,ProductionRunId
    ,tmTimeStamp
    ,sum(Counted)
from
    ProdTable-or-whatever-it-is-called 
group by 
    ProductionId
    ,ProductionRunId
    ,tmTimeStamp
order by 
    ProductionId
    ,ProductionRunId
,tmTimeStamp