Guest User

Untitled

a guest
Oct 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. CREATE TABLE account
  2. (
  3. id number(10) NOT NULL,
  4. account number(20) NOT NULL,
  5. CONSTRAINT account_id PRIMARY KEY (id)
  6. );
  7.  
  8.  
  9. CREATE TABLE account_statements
  10. (
  11. id number(10) NOT NULL,
  12. account_id number(10) NOT NULL,
  13. statement_date date,
  14. inbalance number(20,2),
  15. outbalance number (20,2),
  16. CONSTRAINT statement_id PRIMARY KEY (id),
  17. CONSTRAINT account_statements_foreign_id FOREIGN KEY (account_id) REFERENCES account(id)
  18. );
  19.  
  20.  
  21. CREATE TABLE account_operations
  22. (
  23. id number(10) NOT NULL,
  24. account_id number(10) NOT NULL,
  25. operdate date,
  26. summ number(20,2),
  27. opertype char(6),
  28. CONSTRAINT operations_id PRIMARY KEY (id),
  29. CONSTRAINT account_operations_foreign_id FOREIGN KEY (account_id) REFERENCES account(id)
  30. );
  31.  
  32. CREATE OR REPLACE FUNCTION Get_debet_on_period (
  33. v_startdate IN date,
  34. v_enddate IN date,
  35. v_account IN account.account%TYPE)
  36. RETURN number
  37. IS
  38. v_debet_summ number(20,2);
  39. BEGIN
  40. SELECT SUM(summ) INTO v_debet_summ
  41. FROM account_operations ao,
  42. account a
  43. WHERE ao.operdate between v_startdate AND v_enddate
  44. AND ao.opertype='DEBET'
  45. AND a.account=v_account
  46. AND ao.account_id=a.id;
  47.  
  48. RETURN v_debet_summ;
  49. END;
  50.  
  51. CREATE OR REPLACE FUNCTION Get_credit_on_period (
  52. v_startdate IN date,
  53. v_enddate IN date,
  54. v_account IN account.account%TYPE)
  55. RETURN number
  56. IS
  57. v_credit_summ number(20,2);
  58. BEGIN
  59. SELECT SUM(summ) INTO v_credit_summ
  60. FROM account_operations ao,
  61. account a
  62. WHERE ao.operdate between v_startdate AND v_enddate
  63. AND ao.opertype='CREDIT'
  64. AND a.account=v_account
  65. AND ao.account_id=a.id;
  66. RETURN v_credit_summ;
  67. END;
  68.  
  69. CREATE OR REPLACE FUNCTION Get_balance_on_date (
  70. v_date IN date,
  71. v_account IN account.account%TYPE)
  72. RETURN number
  73. IS
  74. v_balance_summ number(20,2);
  75. v_startdate date;
  76. v_startsumm number;
  77. BEGIN
  78. SELECT MAX(as.statement_date) INTO v_startdate
  79. FROM account_statements as, account a
  80. WHERE a.id=as.account_id
  81. AND a.account=v_account
  82. AND as.statement_date<v_date;
  83. IF v_startdate IS NOT NULL THEN
  84. SELECT as.outbalance INTO v_startsumm
  85. FROM account_statement as, account a
  86. WHERE a.id=as.account_id
  87. AND a.account=v_account
  88. AND as.statement_date=v_statement_date;
  89. v_balance_summ:=v_startsumm+Get_credit_on_period(v_startdate, v_date, v_account)-Get_debet_on_period(v_startdate, v_date, v_account);
  90. ELSE
  91. v_startsumm:=0;
  92. v_balance_summ:=Get_credit_on_period(v_startdate, v_date, v_account)-Get_debet_on_period(v_startdate, v_date, v_account);
  93. END IF;
  94.  
  95. RETURN v_balance_summ;
  96. END;
Add Comment
Please, Sign In to add comment