简体   繁体   English

GROUP BY无法处理时间戳数据-Oracle SQL

[英]GROUP BY not working on time stamp data - Oracle SQL

I have some data regarding some of our work orders. 我有一些有关我们某些工作指令的数据。 The data looks like this: 数据如下所示:

| OrderNumber | StepNumber |  StepStart  |  StepFinish |
|:-----------:|:----------:|:-----------:|:-----------:|
|     100     |      1     |  9:00:00 AM | 12:00:00 PM |
|     100     |      2     | 12:00:00 PM |  1:00:00 PM |
|     100     |      3     |  1:30:00 PM |  1:35:00 PM |
|     100     |      4     |  1:50:00 PM |  5:00:00 PM |
|     100     |      5     |  8:45:00 PM | 11:55:00 PM |
|     200     |      1     |  5:00:00 AM |  5:30:00 AM |
|     200     |      2     |  6:00:00 AM |  6:30:00 AM |
|     200     |      3     |  7:00:00 AM |  7:30:00 AM |
|     200     |      4     |  8:00:00 AM |  8:30:00 AM |
|     200     |      5     |  9:00:00 AM |  9:30:00 AM |
|     200     |      6     | 10:00:00 AM | 10:30:00 AM |
|     200     |      7     | 11:00:00 AM | 11:30:00 AM |
|     200     |      8     | 12:00:00 PM | 12:30:00 PM |

I am trying to GROUP BY the OrderNumber to create the "total time" it took to finish by subtracting the finish time of it's LAST step and the start time of its FIRST step. 我想GROUP BY的ORDERNUMBER打造的“总时间”花减去结束时间是最后一步,它的第一个步骤的开始时间来完成。

My end result would look like this: 我的最终结果将如下所示:

| OrderNumber | TimetoFinish |
|:-----------:|:------------:|
|     100     |     0.62     |
|     200     |    0.3125    |

(Yes, I know those decimals are not time. That is the total "days" it took to finish as sometimes the orders can take multiple days. The real data is a date and timestamp, this is spoofed data. Same rules apply) (是的,我知道这些小数点不是时间。这是完成所需的总“天数”,因为有时订单可能需要数天。真实数据是日期和时间戳,这是欺骗数据。适用相同规则)

I have tried the following: 我尝试了以下方法:

SELECT
  OrderNumber,
  MEDIAN(ABS(MIN(StepStart - StepFinish))) AS TimetoFinish

FROM
  orders

GROUP BY
  OrderNumber

But get an: 但是得到一个:

ORA-00937: not a single-group group function ORA-00937:不是单组分组功能

Which I don't understand, because I am only selecting one non-aggregate column (the OrderNumber ). 我不了解,因为我只选择了一个非聚合列( OrderNumber )。

I have an SQL Fiddle here. 我在这里有一个SQL Fiddle Why is the GROUP BY not working? 为什么GROUP BY不起作用?

Where is the median() coming from? median()来自哪里? Just use: 只需使用:

SELECT OrderNumber,
       (MAX(StepFinish) - MIN(StepStart)) AS TimetoFinish
FROM orders o
GROUP BY OrderNumber;

This assumes that StepFinish and StepStart are stored in some reasonable format. 假定StepFinishStepStart以某种合理的格式存储。 The above should work on date and timestamp for instance. 例如,上面的代码应该在datetimestamp上起作用。

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

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