17代理收入.sql 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. DROP VIEW IF EXISTS view_agent_income;
  11. CREATE OR REPLACE VIEW view_agent_income AS
  12. WITH
  13. trans_base AS (
  14. SELECT wtr.district /* 区域 */, wtr.trans_time /* 交易时间 */, wtr.wallet_id /* 钱包ID */, wtr.amount /* 交易金额 */, w.owner_id /* 所属主体编号 */
  15. FROM
  16. wallet_trans_records wtr
  17. INNER JOIN wallet w ON w.id = wtr.wallet_id
  18. WHERE
  19. wtr.role = 6
  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
  30. trans_base tb
  31. INNER JOIN agent_infos ai ON ai.id = tb.owner_id
  32. AND ai.deleted_at IS NULL
  33. INNER JOIN member_users mu ON mu.id = ai.user_id
  34. AND mu.deleted_at IS NULL
  35. ORDER BY tb.district, tb.trans_time DESC;
  36. select * from view_agent_income;