0

我目前正在尝试查询仓库中 PLC 上发生的前 15 个故障的列表。我已经把那部分记下来了:

Select top 15 fault_number, fault_message, count(*) FaultCount
from Faults_Stator
where T_stamp> dateadd(hour, -18, getdate())
Group by Fault_number, Fault_Message
Order by Faultcount desc

HOOOWEVER 我现在需要找出前 15 个列表中所述故障的累积停机时间,另一列“Fault_duration”中的信息。我该怎么做呢?在此先感谢,你们已经帮了我很多。

+--------------+---------------------------------------------+------------+
| Fault Number |                Fault Message                | FaultCount |
+--------------+---------------------------------------------+------------+
|          122 | ST10: Part A&B Failed                       |         23 |
|            4 | ST16: Part on Table B                       |         18 |
|            5 | ST7: No Spring Present on Part A            |         15 |
|            6 | ST7: No Spring Present on Part B            |         12 |
|            8 | ST3: No Pin Present B                       |          8 |
|            1 | ST5: No A Housing                           |          5 |
|           71 | ST4: Shuttle Right Not Loaded               |          4 |
|          144 | ST15: Vertical Cylinder did not Retract     |          3 |
|           98 | ST8: Plate Loader Can not Retract           |          3 |
|           72 | ST4: Shuttle Left Not Loaded                |          2 |
|           94 | ST8: Spring Gripper Cylinder did not Extend |          2 |
|           60 | ST8: Plate Loader Can not Retract           |          1 |
|           83 | ST6: No A Spring Present                    |          1 |
|            2 | ST5: No B Housing                           |          1 |
|           51 | ST4: Vertical Cylinder did not Extend       |          1 |
+--------------+---------------------------------------------+------------+

我知道我不会使用相同的查询,但我不知道下一步该怎么做。

故障持续时间是一列,指示故障持续多长时间(以毫秒为单位)。我正在尝试将那些累积到相应的故障旁边。因此,第一个违规者将在另一列中将这 23 个单独的故障事件汇总在其旁边。

4

1 回答 1

0

您应该能够使用 SUM 累加器:

Select top 15 fault_number, fault_message, count(*) FaultCount, SUM (Fault_duration) as FaultDuration
from Faults_Stator
where T_stamp> dateadd(hour, -18, getdate())
Group by Fault_number, Fault_Message
Order by Faultcount desc
于 2017-12-15T22:28:31.570 回答