Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. CREATE OR REPLACE FUNCTION get_plan_score(faculty_id integer, from_date date, until_date date)
  2. RETURNS TABLE(
  3. fdate date ,
  4. udate date ,
  5. score int ) AS
  6. $BODY$
  7. DECLARE res integer;
  8. DECLARE unit integer;
  9. DECLARE StartDate DATE;
  10. DECLARE EndofYear DATE;
  11. DECLARE EndDate DATE;
  12. BEGIN
  13. StartDate = from_date ::DATE ;
  14. CREATE TEMP TABLE IF NOT EXISTS temp_plan_score_report(user_id integer,fdate date ,udate date ,score int) ON COMMIT DROP;
  15.  
  16. WHILE (StartDate <= until_date) LOOP
  17. EndofYear = date_trunc('year', StartDate)+ interval '1 year'- interval '1 day';
  18. IF EndofYear > until_date THEN
  19. EndDate = until_date;
  20. ELSE
  21. EndDate = EndofYear;
  22. END IF;
  23. SELECT SUM(general_settings_granttypeamounts.amount) INTO unit
  24. FROM general_settings_granttypeamounts
  25. WHERE general_settings_granttypeamounts.grant_type= 34;
  26. SELECT SUM(grant_granttransaction.amount) INTO res
  27. FROM grant_granttransaction
  28. WHERE grant_granttransaction.date_created >= date(StartDate) AND
  29. grant_granttransaction.date_created <= date(EndOfYear) AND grant_granttransaction.faculty_info_id = $1
  30. AND grant_granttransaction.type = 17
  31. GROUP BY grant_granttransaction.faculty_info_id;
  32.  
  33. IF res IS NULL THEN
  34. INSERT INTO temp_plan_score_report(user_id,fdate,udate,score)
  35. VALUES (faculty_id,StartDate,EndDate,0);
  36. ELSE
  37. INSERT INTO temp_plan_score_report(user_id,fdate,udate,score)
  38. VALUES (faculty_id,StartDate,EndDate, res/unit::float);
  39. END IF;
  40. StartDate = EndDate+1;
  41. END LOOP;
  42. RETURN QUERY SELECT * FROM temp_plan_score_report where temp_plan_score_report.faculty_info_id=user_id;
  43. END;$BODY$
  44. -------------------------------------------------------------
  45.  
  46. LANGUAGE plpgsql VOLATILE
  47. COST 100;
  48. ALTER FUNCTION get_plan_score(integer, date, date)
  49. OWNER TO saed;
  50. COMMENT ON FUNCTION get_plan_score(integer, date, date) IS 'calculate score for practical plans after 92 based on their granttransaction record';
  51.  
  52. -- SELECT * FROM get_plan_score(38585,'2013-02-09':: date ,'2015-05-09' :: date);
  53. -- SELECT * FROM get_plan_score(38585,'2000-02-09':: date ,'2005-05-09' :: date);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement