1

私は Oracle を初めて使用し、まだ 3 か月ほどしか経っていないと思いますが、今は少し問題があります。マシンのダウンタイムを表示するクエリを実行しています。開始/終了時間とその差が表示されます。機械加工は 2 つのシフトで実行されます。シフト 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 です。シフトの最後の項目は、シフト A では常に 18:00 に、シフト B では 6:00 に終了する必要があります。以下のようなものを達成したいと考えています。


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 に答える