58 lines
1.5 KiB
SQL
58 lines
1.5 KiB
SQL
|
|
-- name: GetCompanyWiseReport :many
|
|
SELECT
|
|
b.company_id,
|
|
c.name AS company_name,
|
|
COUNT(*) AS total_bets,
|
|
COALESCE(SUM(b.amount), 0) AS total_cash_made,
|
|
COALESCE(
|
|
SUM(
|
|
CASE
|
|
WHEN sb.cashed_out THEN b.amount -- use actual cashed_out flag 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 companies c ON b.company_id = c.id
|
|
JOIN shop_bets sb ON sb.id = b.id -- join to get cashed_out
|
|
WHERE b.created_at BETWEEN sqlc.arg('from') AND sqlc.arg('to')
|
|
GROUP BY b.company_id, c.name;
|
|
|
|
-- name: GetBranchWiseReport :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 sqlc.arg('from') AND sqlc.arg('to')
|
|
GROUP BY b.branch_id, br.name, br.company_id;
|