Changes between Version 1 and Version 2 of AdvancedReport15


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

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedReport15

    v1 v2  
    1 ===== Трендови на трошење според тагови за цел систем
     1==== Трендови на трошење според тагови за цел систем
    22Трендови на трошење за секој таг во последните шест месеци
    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 tg
    10 JOIN tag_assigned_to_transaction tat ON tg.tag_id = tat.tag_id
    11 JOIN transaction t ON tat.transaction_id = t.transaction_id
    12 JOIN transaction_breakdown tb ON t.transaction_id = tb.transaction_id
    13 WHERE
    14     t.date >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '6 MONTHS')
    15 GROUP BY
    16     tg.tag_name, month
    17 ORDER BY
    18     tg.tag_name, month;
     4CREATE OR REPLACE FUNCTION get_spending_trends_last_6_months()
     5RETURNS TABLE (
     6    tag_name TEXT,
     7    month DATE,
     8    total_spent NUMERIC
     9)
     10LANGUAGE plpgsql
     11AS $$
     12BEGIN
     13    RETURN QUERY
     14    SELECT
     15        tg.tag_name,
     16        DATE_TRUNC('month', t.date) AS month,
     17        COALESCE(SUM(tb.spent_amount), 0) AS total_spent
     18    FROM tag tg
     19    JOIN tag_assigned_to_transaction tat
     20        ON tg.tag_id = tat.tag_id
     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    WHERE t.date >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '6 MONTHS')
     26    GROUP BY tg.tag_name, month
     27    ORDER BY tg.tag_name, month;
     28END;
     29$$;
    1930}}}
     31
     32==== Релациона алгебра
     33- TG(tag_id, tag_name)
     34- TAT(tag_id, transaction_id)
     35- T(transaction_id, date)
     36- TB(transaction_id, spent_amount)
     37
     38JOIN на сите табели:
     39- J1 ← TG ⨝,,TG.tag_id = TAT.tag_id,, TAT
     40- J2 ← J1 ⨝,,TAT.transaction_id = T.transaction_id,, T
     41- J3 ← J2 ⨝,,T.transaction_id = TB.transaction_id,, TB
     42
     43Филтрирање на трансакции од последните 6 месеци:
     44- F ← σ,,date >= DATE_TRUNC('month', CURRENT_DATE - INTERVAL '6 MONTHS'),,(J3)
     45
     46Групирање по таг и месец:
     47- G ← γ,,tag_name, month = DATE_TRUNC('month', date); Σ(spent_amount) → total_spent,,(F)
     48
     49Подредување по таг и месец:
     50- R_final ← τ,,tag_name, month,,(G)