用于计算服务年数的SQL代码

时间:2013-03-28 02:36:49

标签: sql oracle

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

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

以下是2013年3月31日星期日查询日期为2013年3月27日查询周年纪念日的查询。

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)

3 个答案:

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

答案 1 :(得分:0)

您应该真正修复查询以使用ANSI标准联接和有意义的别名(px可能per_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)

答案 2 :(得分:0)

这可能会有所帮助 - 基于emp表的一般示例。在2013年3月的聘用日期和最后一天之间的数年和数月。如果我正确地理解了问题,那么SMITH在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 --
...
...