我对 Oracle 非常陌生,我想这才 3 个月左右,现在我遇到了一些问题。我正在运行一个查询,它将显示机器的停机时间。它将显示开始/结束时间及其差异。加工分两个班次进行,班次 A 为早上 6:00 至下午 6:00,班次 B 为下午 6:00 至早上 6:00。以下是我的查询的示例结果集:
Shift StartDateTime EndDateTime DifferenceInMinutes
B 09/25/2012 17:38:40 09/25/2012 19:12:26 93.7667
B 09/25/2012 19:12:26 09/25/2012 19:25:03 12.6167
B 09/25/2012 19:25:03 09/26/2012 05:55:08 630.0833
A 09/26/2012 05:55:08 09/26/2012 09:19:55 24.8
DifferenceInMinutes 中的总数应始终为每班 720。我所做的是,在每次轮班开始时,轮班 A 的时间是早上 6:00,轮班 B 的时间是 18:00。我使用 Case When 来做到这一点。下面将是结果集的另一个示例:
Shift StartDateTime EndDateTime DifferenceInMinutes
B 09/25/2012 18:00:00 09/25/2012 19:12:26 72.43333
B 09/25/2012 19:12:26 09/25/2012 19:25:03 12.6167
B 09/25/2012 19:25:03 09/26/2012 05:55:08 630.0833
A 09/26/2012 06:00:00 09/26/2012 09:19:55 199.9167
现在我唯一的问题是 EndDateTime,一个班次的最后一项应该总是在 18:00 结束 Shift A 和 6:00 为 Shift B。我想实现如下所示:
Shift StartDateTime EndDateTime DifferenceInMinutes
B 09/25/2012 18:00:00 09/25/2012 19:12:26 72.43333
B 09/25/2012 19:12:26 09/25/2012 19:25:03 12.6167
B 09/25/2012 19:25:03 09/26/2012 06:00:00 634.95
A 09/26/2012 06:00:00 09/26/2012 09:19:55 199.9167
我必须承认我不知道如何使用 Case When,就像我在 StartDateTime 上所做的那样,所以我真的希望有办法解决这个问题。多谢你们。我希望我在这里说得通。这是我到目前为止所拥有的:
SQL >SELECT
> DISTINCT Hm.Txndate
, hm.shiftname AS Shift
, rd.resourcename AS TesterID
, case
when Rsh.Oldlaststatuschangedate < to_char(Rsh.Oldlaststatuschangedate, 'MM/DD/YYYY') || ' 06:00:00' and Rsh.Laststatuschangedate > to_char(Rsh.Laststatuschangedate, 'MM/DD/YYYY') || ' 06:00:00' then to_char(Rsh.Oldlaststatuschangedate, 'MM/DD/YYYY') || ' 06:00:00'
when Rsh.Oldlaststatuschangedate < to_char(Rsh.Oldlaststatuschangedate, 'MM/DD/YYYY') || ' 18:00:00' and Rsh.Laststatuschangedate > to_char(Rsh.Laststatuschangedate, 'MM/DD/YYYY') || ' 18:00:00' then to_char(Rsh.Oldlaststatuschangedate, 'MM/DD/YYYY') || ' 18:00:00'
else to_char(Rsh.Oldlaststatuschangedate)
end as StartDateTime
, lead(hm.shiftname, 1, 0) over (partition by hm.txndate order by rd.resourcename, c.containername, hm.txndate, hm.shiftname) as NextShift
, to_char(Rsh.Laststatuschangedate, 'MM/DD/YYYY') || ' ' || to_char(Rsh.Laststatuschangedate, 'HH24:MI:SS') as EndDateTime
, ROUND((((TO_DATE(rsh.laststatuschangedate, 'MM/DD/YYYY HH24:MI:SS') - TO_DATE(case
when Rsh.Oldlaststatuschangedate < to_char(Rsh.Oldlaststatuschangedate, 'MM/DD/YYYY') || ' 06:00:00' and Rsh.Laststatuschangedate > to_char(Rsh.Laststatuschangedate, 'MM/DD/YYYY') || ' 06:00:00' then to_char(Rsh.Oldlaststatuschangedate, 'MM/DD/YYYY') || ' 06:00:00'
when Rsh.Oldlaststatuschangedate < to_char(Rsh.Oldlaststatuschangedate, 'MM/DD/YYYY') || ' 18:00:00' and Rsh.Laststatuschangedate > to_char(Rsh.Laststatuschangedate, 'MM/DD/YYYY') || ' 18:00:00' then to_char(Rsh.Oldlaststatuschangedate, 'MM/DD/YYYY') || ' 18:00:00'
else to_char(Rsh.Oldlaststatuschangedate)
end)) * 24) * 60), 4) AS TotalMinutes
, Rsh.Newstatusname AS Status
, Rsh.Newreasonname AS StatusReason
, c.containername AS Lot
, pb.productname AS ProductName
, aep.paramvalue AS HandlerID
, rd.attr_09 AS HandlerType
, (SELECT e.employeename AS OperatorID FROM employee e WHERE e.employeeid = hm.employeeid) AS OperatorID
, (SELECT e.fullname AS EmployeeName FROM employee e WHERE e.employeeid = hm.employeeid) AS EmployeeName
, hm.comments AS Remarks
FROM Historymainline hm
INNER JOIN Resourcestatushistory rsh ON hm.Historymainlineid = rsh.Historymainlineid
INNER JOIN ResourceDef rd ON rd.resourceid = rsh.historyid
INNER JOIN a_equipmentparams aep ON rd.resourceid = aep.resourceid
INNER JOIN a_paramname apn ON aep.paramnameid = apn.paramnameid AND apn.paramnamename ='HANDLERID'
INNER JOIN A_WIPEquipment awe ON rd.resourceid = awe.equipmentid
INNER JOIN container c ON c.ContainerId = awe.ContainerId
INNER JOIN Product p ON c.productid = p.productid
INNER JOIN productbase pb ON p.productbaseid = pb.productbaseid
WHERE rd.Resourcename = :equipID
ORDER BY c.containername, hm.txndate;