Changes between Version 1 and Version 2 of AdvancedReport20


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

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedReport20

    v1 v2  
    1 ===== Трансакции според тагови со највисоко трошење за цел систем
     1==== Трансакции според тагови со највисоко трошење за цел систем
    22{{{#!sql
    3 SELECT
    4     tg.tag_name,
    5     SUM(tb.spent_amount) AS total_spent
    6 FROM
    7     transaction_breakdown tb
    8 JOIN transaction t ON tb.transaction_id = t.transaction_id
    9 JOIN tag_assigned_to_transaction tat ON t.transaction_id = tat.transaction_id
    10 JOIN tag tg ON tat.tag_id = tg.tag_id
    11 GROUP BY
    12     tg.tag_name
    13 ORDER BY
    14     total_spent DESC;
     3CREATE OR REPLACE FUNCTION get_total_spent_by_tag_system()
     4RETURNS TABLE (
     5    tag_name TEXT,
     6    total_spent NUMERIC
     7)
     8LANGUAGE plpgsql
     9AS $$
     10BEGIN
     11    RETURN QUERY
     12    SELECT
     13        tg.tag_name,
     14        COALESCE(SUM(tb.spent_amount), 0) AS total_spent
     15    FROM transaction_breakdown tb
     16    JOIN transaction t
     17        ON tb.transaction_id = t.transaction_id
     18    JOIN tag_assigned_to_transaction tat
     19        ON t.transaction_id = tat.transaction_id
     20    JOIN tag tg
     21        ON tat.tag_id = tg.tag_id
     22    GROUP BY tg.tag_name
     23    ORDER BY total_spent DESC;
     24END;
     25$$;
    1526}}}
     27
     28==== Релациона алгебра
     29- TB(transaction_id, spent_amount)
     30- T(transaction_id, date)
     31- TAT(tag_id, transaction_id)
     32- TG(tag_id, tag_name)
     33
     34JOIN на сите табели:
     35- J1 ← TB ⨝,,TB.transaction_id = T.transaction_id,, T
     36- J2 ← J1 ⨝,,T.transaction_id = TAT.transaction_id,, TAT
     37- J3 ← J2 ⨝,,TAT.tag_id = TG.tag_id,, TG
     38
     39Групирање и агрегација по таг:
     40- G ← γ,,tag_name; SUM(spent_amount) → total_spent,,(J3)
     41
     42Подредување по вкупно потрошено:
     43- R_final ← τ,,total_spent DESC,,(G)