【问题标题】:Count number of leave days according to location按地点统计请假天数
【发布时间】:2013-02-26 16:15:54
【问题描述】:

该程序是创建一个包计数的。员工根据组织的位置休假的数量。 例如:- 如果 XYZ 公司是一家位于许多地点的公司。每个地点都有自己的一套规则……在某些地点,周六正在工作,而在某些地点,周六和周日都被视为假期。 我已经制作了如下的包,但我认为它可以更容易。

Create or replace package Body
Leave_location
Procedure Leave_location_proc(Person_id number,
                              Start_date Date
                              )
as 
k Number;
count_weekend number;

Cursor c_var
is 
select pt.person_id,
lt.end_date-lt.start_date Number_of_leaves_excluding_the_end_date,
lt.Start_date      //Start leave date,
lt.Saturday      // 1 if saturday is not working, null if working 
lt.Sunday          ,
loc.location_id
from person_table pt,
leave_table lt
and leave_table.person_id=person_table.person_id
and loc.location_id=pt.person_id \\filtering the location for the particular employee;
Begin

Count_weekend :=0;
       for cursor_var in c_var
       k :=cursor_var.Number_of_leaves_excluding_the_end_date+1
       loop
         for i in..k
         loop
          if(to_char(cursor_var.start_date,'DY') in ('Sat','Sun')
          Then
          if(cursor_var.Saturday is not null and cursor_var.sunday is not null))
          Then
          Count_weekend :=2;
          Else
          count_weekend :=1;
          end if ;
          end if;
       cursor_var.Start_date :=cursor_var.Start_date+1;
No_of_leaves :=K-Count_weekends;
Count_weekends:=Count_weekends+1;
end loop;
end loop;

End Leave_location;
Leave_location_proc;

这段代码似乎效率不高。还有更好的方法

【问题讨论】:

  • 这看起来不像是最终运行的代码,但为什么你认为它效率低下?
  • 这个“感觉”像是一个查询就可以完成的事情,但是你的光标不是很清楚......你能告诉我们你的表结构并给出一些输入示例吗?
  • this post,它可能会给你一些想法
  • @A.B.Cade 实际上这些表是内置的...... oracle hrms 表

标签: sql oracle plsql plsqldeveloper oracle-apps


【解决方案1】:

您不需要为此使用 PL/SQL。这就是我的建议 - 这只是可以转换为游标的查询。您可以计算查询中所需的所有内容。然后,如果需要,您可以在光标中使用它:

-- calculate values from inner/main query
SELECT all_needed_columns_from_inner_query
    , (Number_of_leaves_excluding_the_end_date - nbr_of_wkends) whatever_count
  FROM
 ( -- your main query --
  SELECT ...
       , lt.end_date-lt.start_date Number_of_leaves_excluding_the_end_date
       , (CASE WHEN lt.Saturday IS NOT NULL AND lt.Sunday IS NOT NULL THEN 2 ELSE 1) nbr_of_wkends
    ...
 WHERE...
 )

如果您需要更多帮助,请发布示例数据。但是上面的例子应该给你明确的方向和想法——使用 CASE 来计算你需要的所有值。仅在 PL/SQL 中计算那些无论出于何种原因绝对无法在 SQL 中计算的值...

【讨论】:

    猜你喜欢
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多