Changes between Version 1 and Version 2 of AdvancedReport16


Ignore:
Timestamp:
12/29/25 19:57:04 (4 days ago)
Author:
211101
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedReport16

    v1 v2  
    1 ===== Трендови на трошење според тагови за корисник
     1==== Трендови на трошење според тагови за корисник
    22Сумирање на трошењата според тагови во последните 6 месеци за одреден корисник
    33{{{#!sql
    4 SELECT
    5     tg.tag_name,
    6     DATE_TRUNC('month', t.date) AS month,
    7     SUM(tb.spent_amount) AS total_spent
    8 FROM
    9     tag_assigned_to_transaction tat
    10 JOIN transaction t ON tat.transaction_id = t.transaction_id
    11 JOIN transaction_breakdown tb ON t.transaction_id = tb.transaction_id
    12 JOIN transaction_account ta ON tb.transaction_account_id = ta.transaction_account_id
    13 JOIN tag tg ON tat.tag_id = tg.tag_id
    14 WHERE
    15     ta.user_id = 101 -- ID на конкретниот корисник
    16     AND t.date >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '6 MONTHS')
    17 GROUP BY
    18     tg.tag_name, month
    19 ORDER BY
    20     tg.tag_name, month;
     4CREATE OR REPLACE FUNCTION get_spending_trends_last_6_months_user(
     5    p_user_id INT
     6)
     7RETURNS TABLE (
     8    tag_name TEXT,
     9    month DATE,
     10    total_spent NUMERIC
     11)
     12LANGUAGE plpgsql
     13AS $$
     14BEGIN
     15    RETURN QUERY
     16    SELECT
     17        tg.tag_name,
     18        DATE_TRUNC('month', t.date) AS month,
     19        COALESCE(SUM(tb.spent_amount), 0) AS total_spent
     20    FROM tag_assigned_to_transaction tat
     21    JOIN transaction t
     22        ON tat.transaction_id = t.transaction_id
     23    JOIN transaction_breakdown tb
     24        ON t.transaction_id = tb.transaction_id
     25    JOIN transaction_account ta
     26        ON tb.transaction_account_id = ta.transaction_account_id
     27    JOIN tag tg
     28        ON tat.tag_id = tg.tag_id
     29    WHERE ta.user_id = p_user_id
     30      AND t.date >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '6 MONTHS')
     31    GROUP BY tg.tag_name, month
     32    ORDER BY tg.tag_name, month;
     33END;
     34$$;
    2135}}}
     36
     37==== Релациона алгебра
     38- TA(transaction_account_id, user_id)
     39- T(transaction_id, date)
     40- TB(transaction_id, transaction_account_id, spent_amount)
     41- TAT(tag_id, transaction_id)
     42- TG(tag_id, tag_name)
     43
     44JOIN на сите табели:
     45- J1 ← TA ⨝,,TA.transaction_account_id = TB.transaction_account_id,, TB
     46- J2 ← J1 ⨝,,TB.transaction_id = T.transaction_id,, T
     47- J3 ← J2 ⨝,,T.transaction_id = TAT.transaction_id,, TAT
     48- J4 ← J3 ⨝,,TAT.tag_id = TG.tag_id,, TG
     49
     50Филтрирање по корисник и последни 6 месеци:
     51- F ← σ,,user_id = p_user_id ∧ date >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '6 MONTHS'),,(J4)
     52
     53Групирање по таг и месец:
     54- G ← γ,,tag_name, month = DATE_TRUNC('month', date); Σ(spent_amount) → total_spent,,(F)
     55
     56Подредување по таг и месец:
     57- R_final ← τ,,tag_name, month,,(G)