17代理收入.sql 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. -- Active: xiaoding_test@@192.168.110.85@3306@xiaoding_test
  2. /*
  3. 区域、交易时间、代理商分账都是wallet_trans_records表role字段为代理商角色然后分别获取districe、trans_time、amount字段,
  4. 代理商是wallet_trans_records表role字段为代理商角色然后获取wallet_id字段关联wallet表获取owner_id字段再与agent_infos表关联通过agent_infos表的id进行对比然后获取user_id字段和member_users表关联通过member_users的id对比最后获取nickname字段做为代理商,
  5. 平台分账与代理商分账是一样的数据,市场经费是代理商分账的一半
  6. */
  7. /* 区域、交易时间、代理商、代理商分账、平台分账、市场经费统计 */
  8. DROP TABLE IF EXISTS report_agent_income;
  9. CREATE TABLE report_agent_income AS
  10. WITH trans_base AS (
  11. SELECT
  12. wtr.district /* 区域 */,
  13. wtr.trans_time /* 交易时间 */,
  14. wtr.wallet_id /* 钱包ID */,
  15. wtr.amount /* 交易金额 */,
  16. w.owner_id /* 所属主体编号 */
  17. FROM wallet_trans_records wtr
  18. INNER JOIN wallet w ON w.id = wtr.wallet_id
  19. WHERE wtr.role = '代理商'
  20. AND wtr.deleted_at IS NULL
  21. )
  22. SELECT
  23. tb.district as district /* 区域 */,
  24. tb.trans_time as trans_time /* 交易时间 */,
  25. mu.nickname as nickname /* 代理商昵称 */,
  26. tb.amount as agent_amount /* 代理商分账 */,
  27. tb.amount as platform_amount /* 平台分账 */,
  28. tb.amount / 2 as market_amount /* 市场经费 */
  29. FROM trans_base tb
  30. INNER JOIN agent_infos ai ON ai.id = tb.owner_id AND ai.deleted_at IS NULL
  31. INNER JOIN member_users mu ON mu.id = ai.user_id AND mu.deleted_at IS NULL
  32. ORDER BY
  33. tb.district,
  34. tb.trans_time DESC;