如何追溯更改日期

时间:2018-10-01 15:03:45

标签: oracle oracle-sqldeveloper

我有这个查询,我想让want_date将日期拉回7天,直到。

这是查询:

SELECT
   a.order_num,
   a.is_undated_work,
   To_char(Cast(( From_tz(Cast(ord_order_state.created AS TIMESTAMP), '00:00') AT TIME zone 'EST5EDT' ) AS DATE), 'yyyy-mm-dd hh24:mi:ss' ) AS created,
   area_node.name AS station,
   To_char(Trunc(From_tz(Cast(a.eligible AS TIMESTAMP), '00:00') AT TIME zone 'EST5EDT'), 'yyyy-mm-dd') AS want_date,
   To_char(Trunc(From_tz(Cast(prev_eligible AS TIMESTAMP), '00:00') AT TIME zone 'EST5EDT'), 'yyyy-mm-dd') AS pre_want,
   To_char(Trunc(From_tz(Cast(enroute_at AS TIMESTAMP), '00:00') AT TIME zone 'EST5EDT'), 'yyyy-mm-dd') AS went_to,
   ord_job_code.name AS job_code,
   Nvl(ord_appointment_def.name, 'ANYTM') AS time_slot,
   slr_address_supp AS str_num,
   slr_address AS address,
   slr_city AS city,
   lab_user.logon_id AS asgn_id,
   lab_user.name AS asgn_tech,
   Extractvalue(ord_order_cust_data.order_data, '/OrderRecord/MeterLocCode', 'xmlns="http://www.ventyx.com/ServiceSuite"') AS mtr_loc,
   Extractvalue(ord_order_cust_data.order_data, '/OrderRecord/AMIStatus', 'xmlns="http://www.ventyx.com/ServiceSuite"') AS ami,
   Extractvalue(ord_activity_cust_data.activity_data, '/A03_GFO/MeterTmsCode1', 'xmlns="http://www.ventyx.com/ServiceSuite"') AS tms,
   Extractvalue(ord_order_cust_data.order_data, '/OrderRecord/StdHostJobNmbr', 'xmlns="http://www.ventyx.com/ServiceSuite"') AS host_order_number,
   Extractvalue(ord_activity_cust_data.activity_data, '/A03_GFO/StdCreatorId', 'xmlns="http://www.ventyx.com/ServiceSuite"') AS host_id,
   Replace(Substr( Extractvalue(ord_activity_cust_data.activity_data, '/A03_GFO/StdCreateDT', 'xmlns="http://www.ventyx.com/ServiceSuite"'), 1, 19), 'T', ' ') AS host_create_day,
   Extractvalue(ord_order_cust_data.order_data, '/OrderRecord/StdCustName', 'xmlns="http://www.ventyx.com/ServiceSuite"') AS customer,
   Extractvalue(ord_activity_cust_data.activity_data, '/A03_GFO/CustomerContactPhone', 'xmlns="http://www.ventyx.com/ServiceSuite"') AS contact_phone,
   To_char(Cast(( From_tz(Cast(a.completed AS TIMESTAMP), '00:00') AT TIME zone 'EST5EDT' ) AS DATE), 'yyyy-mm-dd hh24:mi:ss') AS completed,
   To_char(Cast(( From_tz(Cast(a.cancelled AS TIMESTAMP), '00:00') AT TIME zone 'EST5EDT' ) AS DATE), 'yyyy-mm-dd hh24:mi:ss') AS cancelled,
   Extractvalue(ord_activity_cust_data.activity_data, '/A03_GFO/CmpStatusCode', 'xmlns="http://www.ventyx.com/ServiceSuite"') AS comp_status 
FROM
   (
      SELECT
         * 
      FROM
         (
            SELECT
               order_id,
               order_num,
               eligible,
               job_code,
               appointment_type,
               dispatch_area,
               slr_city,
               slr_address,
               slr_address_supp,
               is_undated_work 
            FROM
               ord_order 
            WHERE
               business = 203000000000006 
               AND Trunc(ord_order.eligible) >= Trunc(SYSDATE)
         )
         z 
         join
            (
               SELECT
                  order_state_id,
                  for_order,
                  cancelled,
                  completed 
               FROM
                  ord_order_state
            )
            y 
            ON ( for_order = order_id )
   )
   a 
   left join
      ord_order_state 
      ON ( ord_order_state.for_order = a.order_id ) 
   left join
      ord_order_cust_data 
      ON ( ord_order_cust_data.for_order = a.order_id ) 
   left join
      ord_activity_cust_data 
      ON ( ord_activity_cust_data.for_order = a.order_id ) 
   left join
      ord_job_code 
      ON ( ord_job_code.job_code_id = a.job_code ) 
   left join
      area_node 
      ON ( area_node.node_id = a.dispatch_area ) 
   left join
      ord_appointment_def 
      ON ( ord_appointment_def.appointment_def_id = a.appointment_type ) 
   left join
      asn_assignment 
      ON ( asn_assignment.assignment_id = ord_order_state.latest_assignment ) 
   left join
      lab_resource 
      ON ( lab_resource.resource_id = asn_assignment.for_resource ) 
   left join
      lab_user 
      ON ( lab_user.user_id = lab_resource.for_user ) 
WHERE
   ord_job_code.name IN 
   (
      'MISC_READ',
      'INV_MTR',
      'HEX',
      'INV_FIELD',
      'INV_AMR',
      'EP_HEX_INV',
      'EP_MTR_INV',
      'INV_DR_MTR',
      'DUP_MTRINV' 
   )
;

我是否只需要添加一个want_date语句?

1 个答案:

答案 0 :(得分:0)

这是一个格式错误的查询,因此我使用了在线格式化程序,试图使其更易于阅读。我希望我能成功。

无论如何,是的-您需要在WANT_DATE子句中包含WHERE,例如

where want_date > sysdate - 7

这可能会有所不同,具体取决于WANT_DATE列包含的内容,您是否对时间分量感兴趣(如果不感兴趣,则使用TRUNC或可能使用BETWEEN),等等。,但这是一般想法,应该可以使用。