Advertisement
Guest User

sql

a guest
Jun 16th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. CREATE OR REPLACE PROCEDURE generate_bonus
  2. AS
  3.  
  4. cursor student_info is
  5. select distinct students.id,
  6. events.begindatetime,
  7. events.enddatetime,
  8. count(items.number_of_coupons) as coupons_collected,
  9. events.type from students
  10. join applies on applies.students_id = students.id
  11. join schedules on schedules.id = applies.schedules_id
  12. join events on events.id = schedules.events_id
  13. join orders on orders.students_id = students.id
  14. join orderitems on orderitems.orders_id = orders.id
  15. join items on items.id = orderitems.items_id
  16. join bars on bars.id = orders.bars_id
  17. where applies.status = 'PLANNED'
  18. and orderitems."NUMBER" is not null
  19. and bars.name is not null
  20. group by students.id, events.begindatetime, events.enddatetime, events.type
  21. order by students.id;
  22.  
  23. BEGIN
  24.  
  25. DECLARE
  26. s_id integer(256);
  27. s_beginDate date;
  28. s_endDate date;
  29. s_noCoupons number(256);
  30. s_eventType varchar2(256);
  31. s_workedHours number(24) := 8;
  32. calculated_bonus number(256);
  33. count_rows integer(256);
  34.  
  35. OPEN student_info;
  36.  
  37. LOOP
  38.  
  39. FETCH student_info into s_id, s_beginDate, s_endDate, s_noCoupons, s_eventType;
  40.  
  41. Select count(*) into count_rows from student_bonus where students_id = s_id and rownum <= 1;
  42.  
  43. EXIT WHEN count_rows = 1;
  44.  
  45. IF (s_eventType = 'ROUGH') THEN
  46. calculated_bonus := s_workedHours * (s_workedHours / 100 * 7) * s_noCoupons;
  47.  
  48. INSERT INTO student_bonus(students_id, bonus, events_id) VALUES (s_id, calculated_bonus, s_eventType);
  49.  
  50. calculated_bonus := 0;
  51.  
  52. ELSIF (s_eventType = 'NORMAL') THEN
  53. calculated_bonus := s_workedHours * (s_workedHours / 100 * 4) * s_noCoupons;
  54.  
  55. INSERT INTO student_bonus(students_id, bonus, events_id) VALUES (s_id, calculated_bonus, s_eventType);
  56.  
  57. calculated_bonus := 0;
  58.  
  59. ELSE
  60. calculated_bonus := s_workedHours * (s_workedHours / 100 * 2) * s_noCoupons;
  61.  
  62. INSERT INTO student_bonus(students_id, bonus, events_id) VALUES (s_id, calculated_bonus, s_eventType);
  63.  
  64. calculated_bonus := 0;
  65. END IF;
  66.  
  67. END LOOP;
  68.  
  69. CLOSE student_info;
  70.  
  71. END generate_bonus;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement