Files
yourpart3/backend/sql/diagnostics/falukant_moneyflow_by_activity.sql

32 lines
1.7 KiB
SQL

-- Falukant: Geldbewegungen aus falukant_log.moneyflow (jede Zeile = ein Aufruf von
-- falukant_data.update_money → activity-String wie im Backend übergeben).
--
-- Typische activity-Werte (siehe falukantService.js → updateFalukantUserMoney):
-- Wirtschaft: Production cost; Product sale (net); Sell all products (net);
-- Steueranteil Region (Verkauf); Sales tax (…); Buy/Sell storage (type: …)
-- Transport/Fahrzeuge: transport; build_vehicles; buy_vehicles; repair_vehicle; repair_all_vehicles
-- Filiale: create_branch
-- Haus: housebuy; servants_hired; household_order; renovation_*; renovation_all
-- Soziales: marriage_gift; Marriage cost; Gift cost; partyOrder; Baptism; Reputation action: …
-- Bildung: learnAll; learnItem:<productId>
-- Sonst: new nobility title; health.<aktivität>; credit taken (Kredit = positiver change_value)
--
-- Platzhalter (ersetzt durch scripts/falukant-moneyflow-report.mjs):
-- __DIAG_DAYS__ → positive Ganzzahl (Tage zurück)
-- __DIAG_USER_FILTER__ → leer ODER " AND m.falukant_user_id = <id>"
--
-- Manuell in psql: __DIAG_DAYS__ durch z. B. 30 ersetzen, __DIAG_USER_FILTER__ leer lassen.
SELECT
m.activity,
COUNT(*)::bigint AS n,
ROUND(SUM(m.change_value)::numeric, 2) AS sum_change,
ROUND(SUM(CASE WHEN m.change_value < 0 THEN -m.change_value ELSE 0 END)::numeric, 2) AS total_outflow,
ROUND(SUM(CASE WHEN m.change_value > 0 THEN m.change_value ELSE 0 END)::numeric, 2) AS total_inflow,
ROUND(AVG(m.change_value)::numeric, 4) AS avg_change
FROM falukant_log.moneyflow m
WHERE m."time" >= NOW() - (INTERVAL '1 day' * __DIAG_DAYS__)
__DIAG_USER_FILTER__
GROUP BY m.activity
ORDER BY sum_change ASC NULLS LAST;