Changes between Version 1 and Version 2 of AdvancedReport21


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

--

Legend:

Unmodified
Added
Removed
Modified
  • AdvancedReport21

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