Files
youlegames/codes/games/sql/game/scripts/transfer_summary.sql

50 lines
2.0 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- ============================================================
-- 脚本名称: transfer_summary.sql
-- 功 能: 指定年月的转卡汇总报表(按下级代理分组,含合计行)
-- 数据库 : agent_db
-- 用 法:
-- 1. 修改下方【参数设置】中的变量值
-- 2. 在 Navicat 中执行全部脚本
-- 3. 结果集右键 → Export → Excel 可导出表格
-- ============================================================
-- ============================================================
-- 【参数设置】 ← 修改这里
-- ============================================================
SET @p_year = 2026; -- 年份
SET @p_month = 3; -- 月份1-12
SET @p_from_sales = 10437216; -- 发卡来源代理ID上级代理
SET @p_agent_id = 'veRa0qrBf0df2K1G4de2tgfmVxB2jxpv'; -- 代理商ID
-- ============================================================
-- 【汇总报表】按下级代理分组,最后一行为转卡总计
-- ============================================================
SELECT
IF(
GROUPING(t.satr_salesid),
'★ 合计',
CAST(t.satr_salesid AS CHAR)
) AS '代理ID',
IF(
GROUPING(t.satr_salesid),
'',
MAX(su.saus_nickname)
) AS '代理昵称',
COUNT(t.idx) AS '转卡次数',
SUM(t.satr_amount) AS '转卡总量'
FROM
sales_transferbill t
LEFT JOIN sales_user su
ON t.satr_agentid = su.saus_agentid
AND t.channel_id = su.saus_channelid
AND t.satr_salesid = su.saus_salesid
WHERE
t.satr_agentid = @p_agent_id
AND t.from_sales = @p_from_sales
AND YEAR(t.satr_transfertime) = @p_year
AND MONTH(t.satr_transfertime) = @p_month
GROUP BY
t.satr_salesid WITH ROLLUP;