1

我对 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;
4

2 回答 2

2

我想你要的是这样的东西?

SQL> select shift, startdatetime, enddatetime, round((enddatetime - startdatetime) * 24 * 60, 4) differenceinminutes
  2    from shifts
  3   order by 2;

S 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            204.7833
A 09/26/2012 09:19:55 09/26/2012 18:19:55                 540
B 09/26/2012 18:19:55 09/26/2012 22:10:26            230.5167
B 09/26/2012 22:10:26 09/27/2012 01:11:00            180.5667
B 09/27/2012 01:11:00 09/27/2012 05:54:08            283.1333

8 rows selected.

SQL>

SQL> select shift, startdatetime, enddatetime,
  2         round((enddatetime - startdatetime) * 24 * 60, 4) differenceinminutes
  3    from (select shift,
  4                  case
  5                    when shift = 'A' and r = 'a' then
  6                     trunc(startdatetime) + (6 / 24)
  7                    when shift = 'B' and r = 'a' then
  8                     trunc(startdatetime) + (18 / 24)
  9                    else
 10                     startdatetime
 11                  end startdatetime,
 12                  case
 13                    when shift = 'A' and r = 'b' then
 14                     trunc(enddatetime) + (18 / 24)
 15                    when shift = 'B' and r = 'b' then
 16                     trunc(enddatetime) + (6 / 24)
 17                    else
 18                     enddatetime
 19                  end enddatetime
 20             from (select shift, startdatetime, enddatetime,
 21                           case
 22                             when lag(shift) over(order by startdatetime) != shift then
 23                              'a'
 24                             when nvl(lead(shift) over(order by startdatetime), 'x') != shift then
 25                              'b'
 26                             when row_number() over(order by startdatetime) = 1 then
 27                              'a'
 28                           end r
 29                      from shifts))
 30   order by startdatetime
 31  /

S STARTDATETIME       ENDDATETIME         DIFFERENCEINMINUTES
- ------------------- ------------------- -------------------
B 09/25/2012 18:00:00 09/25/2012 19:12:26             72.4333
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
A 09/26/2012 09:19:55 09/26/2012 18:00:00            520.0833
B 09/26/2012 18:00:00 09/26/2012 22:10:26            250.4333
B 09/26/2012 22:10:26 09/27/2012 01:11:00            180.5667
B 09/27/2012 01:11:00 09/27/2012 06:00:00                 289

8 rows selected.

http://sqlfiddle.com/#!4/b5d29/3

于 2012-11-05T13:30:09.797 回答
1

能够回答我自己的问题。这是我使用的查询:

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
, case
    when lead(hm.shiftname, 2, 0) over (order by c.containername, hm.txndate) <> hm.shiftname then 
     case
        when lead(hm.shiftname, 2, 0) over (order by c.containername, hm.txndate) = 'A' then to_char(Rsh.Laststatuschangedate, 'MM/DD/YYYY') || ' 06:00:00'
        when lead(hm.shiftname, 2, 0) over (order by c.containername, hm.txndate) = 'B' then to_char(Rsh.Laststatuschangedate, 'MM/DD/YYYY') || ' 18:00:00'
      end
    else to_char(Rsh.Laststatuschangedate)
  end as EndDateTime
, ROUND((((TO_DATE(case
    when lead(hm.shiftname, 2, 0) over (order by c.containername, hm.txndate) <> hm.shiftname then 
     case
        when lead(hm.shiftname, 2, 0) over (order by c.containername, hm.txndate) = 'A' then to_char(Rsh.Laststatuschangedate, 'MM/DD/YYYY') || ' 06:00:00'
        when lead(hm.shiftname, 2, 0) over (order by c.containername, hm.txndate) = 'B' then to_char(Rsh.Laststatuschangedate, 'MM/DD/YYYY') || ' 18:00:00'
      end
    else to_char(Rsh.Laststatuschangedate)
  end) - 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;

感谢大家!我还有一些问题,但我认为我需要为此启动另一个线程。再次感谢所有帮助的家伙。

于 2012-11-06T05:24:47.223 回答