更改Oracle中查询结果集中的值

时间:2012-11-05 12:50:58

标签: oracle

我几乎是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,班次班次是18:00。我使用Case何时能够做到这一点。下面是结果集的另一个例子:


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就像我在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;

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;

谢谢大家!我还有一些问题,但我想我需要为此开始另一个问题。再次感谢所有帮助人员。