| 1 | = Напредни извештаи од базата (SQL и складирани процедури) |
| 2 | |
| 3 | ===== Просечно трошење во последните 3 дена |
| 4 | Просечно дневно трошење за последните три дена, земајќи ги предвид сите трансакции: |
| 5 | {{{#!sql |
| 6 | SELECT |
| 7 | AVG(daily_spending) AS average_spending_last_3_days |
| 8 | FROM ( |
| 9 | SELECT |
| 10 | t.date::date AS transaction_date, |
| 11 | SUM(tb.spent_amount) AS daily_spending |
| 12 | FROM |
| 13 | transaction t |
| 14 | JOIN |
| 15 | transaction_breakdown tb ON t.transaction_id = tb.transaction_id |
| 16 | WHERE |
| 17 | t.date >= CURRENT_DATE - INTERVAL '2 DAY' |
| 18 | AND t.date < CURRENT_DATE + INTERVAL '1 DAY' |
| 19 | GROUP BY |
| 20 | t.date::date |
| 21 | ) daily_totals; |
| 22 | }}} |
| 23 | |
| 24 | ===== Вкупно потрошено во тековниот месец |
| 25 | Вкупна сума на трошоци во тековниот месец |
| 26 | {{{#!sql |
| 27 | SELECT |
| 28 | SUM(tb.spent_amount) AS total_spent |
| 29 | FROM |
| 30 | transaction_breakdown tb |
| 31 | JOIN transaction t ON tb.transaction_id = t.transaction_id |
| 32 | WHERE |
| 33 | EXTRACT(MONTH FROM t.date) = EXTRACT(MONTH FROM CURRENT_DATE) |
| 34 | AND EXTRACT(YEAR FROM t.date) = EXTRACT(YEAR FROM CURRENT_DATE); |
| 35 | }}} |
| 36 | |
| 37 | ===== Просечно дневно трошење за тековниот месец |
| 38 | {{{#!sql |
| 39 | SELECT |
| 40 | SUM(tb.spent_amount) / EXTRACT(DAY FROM CURRENT_DATE) AS average_daily_spending |
| 41 | FROM |
| 42 | transaction_breakdown tb |
| 43 | JOIN transaction t ON tb.transaction_id = t.transaction_id |
| 44 | WHERE |
| 45 | EXTRACT(MONTH FROM t.date) = EXTRACT(MONTH FROM CURRENT_DATE) |
| 46 | AND EXTRACT(YEAR FROM t.date) = EXTRACT(YEAR FROM CURRENT_DATE) |
| 47 | AND t.date <= CURRENT_DATE; |
| 48 | }}} |
| 49 | |
| 50 | ===== Дневен буџет до крајот на месецот |
| 51 | Дневен буџет за преостанатите денови од месецот врз основа на вкупниот баланс на сите сметки |
| 52 | {{{#!sql |
| 53 | SELECT |
| 54 | SUM(ta.balance) / (DATE_PART('day', DATE_TRUNC('month', CURRENT_DATE + INTERVAL '1 MONTH') - INTERVAL '1 DAY') - DATE_PART('day', CURRENT_DATE)) AS daily_budget |
| 55 | FROM |
| 56 | transaction_account ta; |
| 57 | }}} |
| 58 | |
| 59 | ===== Долг на кредитна картичка од минатиот месец |
| 60 | Вкупна сума потрошена од сметките обележани како „кредитна картичка“ за минатиот месец |
| 61 | {{{#!sql |
| 62 | SELECT |
| 63 | SUM(tb.spent_amount) AS credit_card_debt |
| 64 | FROM |
| 65 | transaction_breakdown tb |
| 66 | JOIN transaction_account ta ON tb.transaction_account_id = ta.transaction_account_id |
| 67 | JOIN transaction t ON tb.transaction_id = t.transaction_id |
| 68 | WHERE |
| 69 | (ta.account_name ILIKE '%кредитна%' OR ta.account_name ILIKE '%credit%') |
| 70 | AND EXTRACT(MONTH FROM t.date) = EXTRACT(MONTH FROM CURRENT_DATE - INTERVAL '1 MONTH') |
| 71 | AND EXTRACT(YEAR FROM t.date) = EXTRACT(YEAR FROM CURRENT_DATE - INTERVAL '1 MONTH'); |
| 72 | }}} |
| 73 | |
| 74 | ===== Трендови на трошење според тагови |
| 75 | Трендови на трошење за секој таг во последните шест месеци |
| 76 | {{{#!sql |
| 77 | SELECT |
| 78 | tg.tag_name, |
| 79 | DATE_TRUNC('month', t.date) AS month, |
| 80 | SUM(tb.spent_amount) AS total_spent |
| 81 | FROM |
| 82 | tag tg |
| 83 | JOIN transaction t ON tg.tag_id = t.tag_id |
| 84 | JOIN transaction_breakdown tb ON t.transaction_id = tb.transaction_id |
| 85 | WHERE |
| 86 | t.date >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '6 MONTHS') |
| 87 | GROUP BY |
| 88 | tg.tag_name, month |
| 89 | ORDER BY |
| 90 | tg.tag_name, month; |
| 91 | }}} |
| 92 | |
| 93 | ===== Вкупно трошење според тагови |
| 94 | Вкупно трошење групирано според тагови за тековниот месец |
| 95 | {{{#!sql |
| 96 | SELECT |
| 97 | tg.tag_name, |
| 98 | SUM(tb.spent_amount) AS total_spent |
| 99 | FROM |
| 100 | tag tg |
| 101 | JOIN transaction t ON tg.tag_id = t.tag_id |
| 102 | JOIN transaction_breakdown tb ON t.transaction_id = tb.transaction_id |
| 103 | WHERE |
| 104 | EXTRACT(MONTH FROM t.date) = EXTRACT(MONTH FROM CURRENT_DATE) |
| 105 | AND EXTRACT(YEAR FROM t.date) = EXTRACT(YEAR FROM CURRENT_DATE) |
| 106 | GROUP BY |
| 107 | tg.tag_name |
| 108 | ORDER BY |
| 109 | total_spent DESC; |
| 110 | }}} |
| 111 | |
| 112 | ===== Сметки со највисоко вкупно трошење во изминатата година |
| 113 | {{{#!sql |
| 114 | SELECT |
| 115 | ta.account_name, |
| 116 | SUM(tb.spent_amount) AS total_spent |
| 117 | FROM |
| 118 | transaction_breakdown tb |
| 119 | JOIN transaction t ON tb.transaction_id = t.transaction_id |
| 120 | JOIN transaction_account ta ON tb.transaction_account_id = ta.transaction_account_id |
| 121 | WHERE |
| 122 | t.date >= NOW() - INTERVAL '1 YEAR' |
| 123 | GROUP BY |
| 124 | ta.account_name |
| 125 | ORDER BY |
| 126 | total_spent DESC |
| 127 | LIMIT 10; |
| 128 | }}} |
| 129 | |
| 130 | ===== Најчести времиња за трансакции |
| 131 | Во кој час од денот корисниците најчесто вршат трансакции |
| 132 | {{{#!sql |
| 133 | SELECT |
| 134 | EXTRACT(HOUR FROM t.date) AS transaction_hour, |
| 135 | COUNT(*) AS transaction_count |
| 136 | FROM |
| 137 | transaction t |
| 138 | GROUP BY |
| 139 | transaction_hour |
| 140 | ORDER BY |
| 141 | transaction_count DESC; |
| 142 | }}} |
| 143 | |
| 144 | ===== Трансакции според тагови со највисоко трошење |
| 145 | {{{#!sql |
| 146 | SELECT |
| 147 | tg.tag_name, |
| 148 | SUM(tb.spent_amount) AS total_spent |
| 149 | FROM |
| 150 | transaction_breakdown tb |
| 151 | JOIN transaction t ON tb.transaction_id = t.transaction_id |
| 152 | JOIN tag tg ON t.tag_id = tg.tag_id |
| 153 | GROUP BY |
| 154 | tg.tag_name |
| 155 | ORDER BY |
| 156 | total_spent DESC; |
| 157 | }}} |
| 158 | |
| 159 | ===== Годишни трендови на трансакции |
| 160 | Трошења според сметки за секој квартал од тековната година |
| 161 | {{{#!sql |
| 162 | SELECT |
| 163 | ta.account_name, |
| 164 | CASE WHEN EXTRACT(QUARTER FROM t.date) = 1 THEN SUM(tb.spent_amount) ELSE 0 END AS q1_spent, |
| 165 | CASE WHEN EXTRACT(QUARTER FROM t.date) = 2 THEN SUM(tb.spent_amount) ELSE 0 END AS q2_spent, |
| 166 | CASE WHEN EXTRACT(QUARTER FROM t.date) = 3 THEN SUM(tb.spent_amount) ELSE 0 END AS q3_spent, |
| 167 | CASE WHEN EXTRACT(QUARTER FROM t.date) = 4 THEN SUM(tb.spent_amount) ELSE 0 END AS q4_spent |
| 168 | FROM |
| 169 | transaction_breakdown tb |
| 170 | JOIN transaction t ON tb.transaction_id = t.transaction_id |
| 171 | JOIN transaction_account ta ON tb.transaction_account_id = ta.transaction_account_id |
| 172 | WHERE |
| 173 | EXTRACT(YEAR FROM t.date) = EXTRACT(YEAR FROM NOW()) |
| 174 | GROUP BY |
| 175 | ta.account_name, EXTRACT(QUARTER FROM t.date); |
| 176 | }}} |
| 177 | |
| 178 | ===== Неактивни тагови |
| 179 | Идентификување тагови кои не биле користени во изминатиот месец |
| 180 | {{{#!sql |
| 181 | SELECT |
| 182 | tg.tag_name |
| 183 | FROM |
| 184 | tag tg |
| 185 | LEFT JOIN transaction t ON tg.tag_id = t.tag_id |
| 186 | WHERE |
| 187 | t.date IS NULL OR t.date < NOW() - INTERVAL '1 MONTH'; |
| 188 | }}} |