Yimaru-BackEnd/db/query/events_bet_stats.sql
Samuel Tariku 18689ea124 feat: Add EventWithSettings domain model and related conversion functions
- Introduced EventWithSettings and EventWithSettingsRes structs for enhanced event data handling.
- Implemented conversion functions for creating and updating event settings.
- Added support for fetching events with settings from the database.
- Created new report data structures for comprehensive reporting capabilities.
- Implemented event statistics retrieval and filtering by league and sport.
- Added handlers for event statistics endpoints in the web server.
- Introduced DateInterval type for managing time intervals in reports.
2025-10-18 11:50:17 +03:00

28 lines
935 B
SQL

-- Aggregate bet stats per event
-- name: UpdateEventBetStats :exec
INSERT INTO event_bet_stats (
event_id,
number_of_bets,
total_amount,
avg_bet_amount,
total_potential_winnings,
updated_at
)
SELECT bo.event_id,
COUNT(DISTINCT b.id) AS number_of_bets,
COALESCE(SUM(b.amount), 0) AS total_amount,
COALESCE(AVG(b.amount), 0) AS avg_bet_amount,
COALESCE(SUM(b.potential_win), 0) AS total_potential_winnings,
NOW() AS updated_at
FROM bet_outcomes bo
JOIN bets b ON bo.bet_id = b.id
GROUP BY bo.event_id ON CONFLICT (event_id) DO
UPDATE
SET number_of_bets = EXCLUDED.number_of_bets,
total_amount = EXCLUDED.total_amount,
avg_bet_amount = EXCLUDED.avg_bet_amount,
total_potential_winnings = EXCLUDED.total_potential_winnings,
updated_at = EXCLUDED.updated_at;
-- name: UpdateEventDetailedViewMat :exec
REFRESH MATERIALIZED VIEW event_detailed_mat;