0

我必须创建一个 oracle sql 来识别在下周内达到里程碑周年纪念的员工。我实际上不得不创建两个代码。一个用于识别今年达到里程碑周年纪念的每位员工,第二个用于识别下周达到里程碑周年纪念的任何员工。

因此,如果我这周运行代码,它将捕获下周有周年纪念日的任何人。对于此代码,我能够创建一个代码来识别特定日期的周年纪念日……但我无法创建一个查看下周所有 7 天并告诉我谁都有周年纪念日的代码。

当查询在今天 2013 年 3 月 27 日运行时,这是用于识别在 2013 年 3 月 31 日星期日有周年纪念日的任何人的查询。

SELECT a.full_name AS Full_Name,
       b.adjusted_svc_date AS Service_Date,
       d.name Job_tittle,
       TRUNC ( (TO_NUMBER (TO_CHAR (SYSDATE + 4, 'mmddyyyy'))) -- TO_NUMBER (TO_CHAR (SYSDATE+4, 'MM/DD/YYYY'))
              - TO_NUMBER (TO_CHAR (b.adjusted_svc_date, 'mmddyyyy')))
          AS Years_OF_Service
  FROM per_people_x a,
       per_periods_of_service b,
       per_assignments_x c,
       per_jobs d
WHERE     a.person_id = c.person_id
       AND c.period_of_service_id = b.period_of_service_id
       AND c.job_id = d.job_id
       AND TRUNC (
                (TO_NUMBER (TO_CHAR (SYSDATE + 4, 'mmddyyyy')))
              - TO_NUMBER (TO_CHAR (b.adjusted_svc_date, 'mmddyyyy'))) IN
              (5, 10, 15, 20, 25, 30)
       AND a.person_type_id IN (6, 7)
4

3 回答 3

1

我结合了大家提供的信息并且能够获取代码。这是每个星期一运行代码的代码。

SELECT ppx.full_name AS Full_Name,
       ps.adjusted_svc_date AS Service_Date,
       pj.name Job_tittle,
         (TO_NUMBER (TO_CHAR (SYSDATE, 'YYYY')))
       - TO_NUMBER (TO_CHAR (ps.adjusted_svc_date, 'YYYY'))
          AS Years_OF_Service
  FROM per_peoples_x ppx,
       per_periods_of_services ps,
       per_assignment_x pax,
       per_jobs pj,
       (SELECT 6 AS d FROM DUAL
        UNION ALL
        SELECT 7 FROM DUAL
        UNION ALL
        SELECT 8 FROM DUAL
        UNION ALL
        SELECT 9 FROM DUAL
        UNION ALL
        SELECT 10 FROM DUAL
        UNION ALL
        SELECT 11 FROM DUAL
        UNION ALL
        SELECT 12 FROM DUAL
        ) days_in_future
 WHERE     ppx.person_id = pax.person_id
       AND pax.period_of_service_id = ps.period_of_service_id
       AND pax.job_id = pj.job_id
       AND   (TO_NUMBER (TO_CHAR (SYSDATE, 'YYYY')))
           - TO_NUMBER (TO_CHAR (ps.adjusted_svc_date, 'YYYY')) IN
              (5, 10, 15, 20, 25, 30)
       AND TO_CHAR (SYSDATE + days_in_future.d, 'mmdd') =
              TO_CHAR (adjusted_svc_date, 'mmdd')
       AND ppx.person_type_id IN (6, 7)
于 2013-04-03T20:19:32.453 回答
0

您应该真正修复您的查询以使用 ANSI 标准连接和有意义的别名(可能px是 forper_people_x而不是a.

无论如何,解决问题的一种方法是交叉加入一组数字并在未来几天使用它们。服务年限的计算只是年限之间的差异。周年纪念支票仅使用月份和日期。

SELECT a.full_name AS Full_Name,
       b.adjusted_svc_date AS Service_Date,
       d.name Job_tittle,
       year(sysdate+days_in_future.d) - year(adjusted_svc_date) AS Years_OF_Service
  FROM per_people_x a,
       per_periods_of_service b,
       per_assignments_x c,
       per_jobs d,
       (select 0 as d from dual union all select 1 from dual union all select 2 from dual
       ) days_in_future
WHERE     a.person_id = c.person_id
       AND c.period_of_service_id = b.period_of_service_id
       AND c.job_id = d.job_id
       and to_char(sysdate+days_in_future.d, 'mmdd') = to_char(adjusted_svc_date, 'mmdd')
       and year(sysdate+days_in_future.d) - year(adjusted_svc_date) in (5, 10,15, 20, 25, 30)
       AND a.person_type_id IN (6, 7)
于 2013-03-28T02:46:19.600 回答
0

这可能会有所帮助 - 基于 emp 表的一般示例。从雇用日期到 2013 年 3 月的最后一天之间的年月。如果我正确理解了这个问题,那么史密斯在 2013 年 3 月 31 日之前工作了 32 年零 3 个月,而不是周年纪念日。琼斯工作了 32 年零 0 个月——纪念日!我希望可以合理地假设周年纪念日基于年份和飞蛾,而不是每周。

您可以将月份的最后一天更改为任何日期 - 从今天起一周等...例如:Last_Day(Trunc(SYSDATE))+7 等...结果应该是相同的。

SELECT * FROM 
(
 SELECT ename
      , EXTRACT(YEAR FROM (Last_Day(Trunc(SYSDATE)) - hiredate) YEAR TO MONTH ) years -- years from last day of Mar-2013 --
      , EXTRACT(MONTH FROM (Last_Day(Trunc(SYSDATE)) - hiredate) YEAR TO MONTH ) mnths       -- months from last day of Mar-2013 --
   FROM scott.emp 
 )
WHERE mnths = 0 -- if months = 0 then it is an anniversary --
/

ENAME    YEARS    MNTHS
-------------------------
SMITH    32        3
ALLEN    32        1
WARD     32        1
JONES    32        0  -- Only this row will be returned with Where mnths = 0 --
...
...
于 2013-03-28T13:19:44.770 回答