// Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.29.0 // source: branch_stats.sql package dbgen import ( "context" "github.com/jackc/pgx/v5/pgtype" ) const GetBranchStats = `-- name: GetBranchStats :many SELECT b.branch_id, br.name AS branch_name, br.company_id, COUNT(*) AS total_bets, COALESCE(SUM(b.amount), 0) AS total_cash_made, COALESCE( SUM( CASE WHEN sb.cashed_out THEN b.amount -- use cashed_out from shop_bets ELSE 0 END ), 0 ) AS total_cash_out, COALESCE( SUM( CASE WHEN b.status = 5 THEN b.amount ELSE 0 END ), 0 ) AS total_cash_backs FROM shop_bet_detail b JOIN branches br ON b.branch_id = br.id JOIN shop_bets sb ON sb.id = b.id -- join to get cashed_out WHERE b.created_at BETWEEN $1 AND $2 GROUP BY b.branch_id, br.name, br.company_id ` type GetBranchStatsParams struct { From pgtype.Timestamp `json:"from"` To pgtype.Timestamp `json:"to"` } type GetBranchStatsRow struct { BranchID int64 `json:"branch_id"` BranchName string `json:"branch_name"` CompanyID int64 `json:"company_id"` TotalBets int64 `json:"total_bets"` TotalCashMade interface{} `json:"total_cash_made"` TotalCashOut interface{} `json:"total_cash_out"` TotalCashBacks interface{} `json:"total_cash_backs"` } func (q *Queries) GetBranchStats(ctx context.Context, arg GetBranchStatsParams) ([]GetBranchStatsRow, error) { rows, err := q.db.Query(ctx, GetBranchStats, arg.From, arg.To) if err != nil { return nil, err } defer rows.Close() var items []GetBranchStatsRow for rows.Next() { var i GetBranchStatsRow if err := rows.Scan( &i.BranchID, &i.BranchName, &i.CompanyID, &i.TotalBets, &i.TotalCashMade, &i.TotalCashOut, &i.TotalCashBacks, ); err != nil { return nil, err } items = append(items, i) } if err := rows.Err(); err != nil { return nil, err } return items, nil }