source: db-scripts/p6-relational-algebra/p6_q1_annual_finance_budget_resilience_ra.txt@ 708af9e

Last change on this file since 708af9e was 89156c1, checked in by Andrej <asumanovski@…>, 3 months ago

Phase 6 documentation

  • Property mode set to 100644
File size: 2.0 KB
Line 
1FB <- pi_{fu.user_id, u.username, u.email,
2 COALESCE(fu.spending_budget,0)->spending_budget,
3 COALESCE(fu.saving_budget,0)->saving_budget,
4 COALESCE(fu.investing_budget,0)->investing_budget,
5 COALESCE(fu.donation_budget,0)->donation_budget,
6 COALESCE(fu.credit,0)->credit}
7 (finance_users fu bowtie_{fu.user_id = u.user_id} users u)
8
9FBM <- FB x M
10IY <- sigma_{YEAR(i.date)=Y}(incomes i)
11MI0 <- FBM leftouterjoin_{FBM.user_id = i.user_id AND FBM.month_no = MONTH(i.date)} IY
12MI <- gamma_{user_id, month_no;
13 SUM(COALESCE(i.amount,0))->month_income}(MI0)
14
15MIR <- omega_{PARTITION BY user_id ORDER BY month_income DESC, month_no ASC;
16 DENSE_RANK()->best_month_rank,
17 DENSE_RANK(PARTITION BY user_id ORDER BY month_income ASC, month_no ASC)->worst_month_rank}(MI)
18
19AI <- gamma_{user_id;
20 SUM(month_income)->total_income,
21 AVG(month_income)->avg_monthly_income,
22 STDDEV_SAMP(month_income)->income_stddev,
23 MAX(month_income)->best_month_income,
24 MIN(month_income)->worst_month_income,
25 COUNT_IF(month_income>0)->active_income_months}(MI)
26
27BWM <- gamma_{user_id;
28 MAX_IF(month_no, best_month_rank=1)->best_month_no,
29 MAX_IF(month_no, worst_month_rank=1)->worst_month_no}(MIR)
30
31R0 <- FB bowtie_{FB.user_id=AI.user_id} AI bowtie_{FB.user_id=BWM.user_id} BWM
32R1 <- alpha_{(spending_budget+saving_budget+investing_budget+donation_budget)*12->planned_annual_budget,
33 total_income->actual_annual_income,
34 income_stddev/NULLIF(avg_monthly_income,0)->income_volatility_cv,
35 total_income-(spending_budget*12)->annual_free_cash_after_spending,
36 (spending_budget*12)/NULLIF(total_income,0)->spending_pressure_ratio,
37 credit/NULLIF(total_income,0)->leverage_ratio}(R0)
38R <- omega_{ORDER BY annual_free_cash_after_spending DESC,
39 spending_pressure_ratio ASC,
40 user_id ASC;
41 DENSE_RANK()->finance_resilience_rank}(R1)
42
Note: See TracBrowser for help on using the repository browser.