我有一个查询,它给我输出
select distinct(d.data_name) ,h.workstation_name, h.type_id, convert(varchar(19),(h.insert_date),120) as start
from data d, process_data_history h
where h.data_id = d.data_id
and h.type_id between '500' and '5001'
and h.workstation_name like 'PL1W7D-105120'
and CONVERT(VARCHAR(25), h.insert_date, 126) LIKE '2016-04%'
order by d.data_name, start ;
输出看起来像这样
data_name workstation_name type_id start
WE228JP_2016_04_05__10_03_03 PL1W7D-105120 500 2016-04-05 12:45:38
WE228JP_2016_04_05__10_03_03 PL1W7D-105120 501 2016-04-05 12:45:38
WE228JP_2016_04_05__10_03_03 PL1W7D-105120 600 2016-04-05 12:45:39
WE228JP_2016_04_05__10_03_03 PL1W7D-105120 601 2016-04-05 12:45:39
WE228JP_2016_04_05__10_03_03 PL1W7D-105120 4001 2016-04-05 14:06:04
WE228JP_2016_04_05__10_03_03 PL1W7D-105120 4000 2016-04-05 14:05:55
WE228JP_2016_04_05__10_03_03 PL1W7D-105120 4100 2016-04-05 14:06:05
WE228JP_2016_04_05__10_03_03 PL1W7D-105120 4101 2016-04-05 14:10:43
WE228JP_2016_04_05__10_03_03 PL1W7D-105120 5000 2016-04-05 14:10:43
WE228JP_2016_04_05__10_03_03 PL1W7D-105120 5001 2016-04-05 14:10:45
WE228JP_2016_04_06__10_33_03 PL1W7D-105120 500 2016-04-06 14:10:46
WE228JP_2016_04_06__10_33_03 PL1W7D-105120 600 2016-04-06 14:10:47
WE228JP_2016_04_06__10_33_03 PL1W7D-105120 501 2016-04-06 14:10:47
WE228JP_2016_04_06__10_33_03 PL1W7D-105120 601 2016-04-06 14:10:48
WE228JP_2016_04_06__10_33_03 PL1W7D-105120 4000 2016-04-06 15:30:07
WE228JP_2016_04_06__10_33_03 PL1W7D-105120 4001 2016-04-06 15:30:18
WE228JP_2016_04_06__10_33_03 PL1W7D-105120 4100 2016-04-06 15:30:18
WE228JP_2016_04_06__10_33_03 PL1W7D-105120 4101 2016-04-06 15:35:01
WE228JP_2016_04_06__10_33_03 PL1W7D-105120 5000 2016-04-06 15:35:01
WE228JP_2016_04_06__10_33_03 PL1W7D-105120 5001 2016-04-06 15:35:03
WE228JP_2016_04_15__10_03_03 PL1W7D-105122 500 2016-04-05 12:45:38
WE228JP_2016_04_15__10_03_03 PL1W7D-105122 501 2016-04-05 12:45:38
WE228JP_2016_04_15__10_03_03 PL1W7D-105122 600 2016-04-05 12:45:39
WE228JP_2016_04_15__10_03_03 PL1W7D-105120 601 2016-04-05 12:45:39
WE228JP_2016_04_15__10_03_03 PL1W7D-105122 4001 2016-04-05 14:06:04
WE228JP_2016_04_15__10_03_03 PL1W7D-105122 5000 2016-04-05 14:10:43
WE228JP_2016_04_15__10_03_03 PL1W7D-105122 5001 2016-04-05 14:10:45
我也知道
ID 500 600 ... 5000表示步骤(程序)的开始时间
ID 501 601 ... 5001表示步骤(程序)的结束时间
所以我需要知道工作站每天工作多长时间:
所以期望的输出就像这样(例子):
data_name workstation_name Hours
2016-04-05 PL1W7D-105120 22 hours
2016-04-06 PL1W7D-105120 21 hours
2016-04-15 PL1W7D-105122 17 hours
任何暗示我怎么能这样做?什么都行;)
答案 0 :(得分:2)
我认为你只想要聚合。这是一种方法:
select d.data_name, h.workstation_name, h.type_id,
datediff(hour, min(insert_date), max(insert_date)) as hours_sortof
from data d join
process_data_history h
on h.data_id = d.data_id
where h.type_id between '500' and '5001' and
h.workstation_name like 'PL1W7D-105120'
h.insert_date >= '2016-04-01' and h.insert_date < '2016-05-01'
group by d.date_name, h.workstation_name,
order by d.data_name, start ;
注意:
JOIN
语法。 从不在FROM
子句中使用逗号。LIKE
。隐式转换可能是问题的主要来源。hours_sortof
基于SQL Server处理时间差异的方式。它计算小时边界而不是实际小时数。要进一步细化,请使用较小的增量,例如分钟或秒。答案 1 :(得分:0)
将输出数据放入表(或临时表)中,这应该可行。您的数据有一个在结束前开始的事件!
;with x as
(
select data_name, workstation_name, event_id = type_id/10, endEventIndicator = type_id%10, timestamp = start
from Data
)
select Date = cast(starts.timestamp as date), starts.workstation_name, sum(DateDiff(hour, starts.timestamp, ends.timestamp))
from x starts inner join x as ends
on starts.event_id = ends.event_id
and starts.endEventIndicator = 0 and ends.endEventIndicator = 1
and starts.workstation_name = ends.workstation_name
group by cast(starts.timestamp as date), starts.workstation_name