lt1-credit-bd-sum-bal.sql-- 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. truncate table dbt_list_type1;
  2. drop procedure if exists lt1_credit_bd_sum_bal;
  3. DELIMITER //
  4. create procedure lt1_credit_bd_sum_bal( _listToken varchar(21), _sdate varchar(8), _edate varchar(8),
  5. _s1 varchar(32), _e1 varchar(32), _s2 varchar(32), _e2 varchar(32),
  6. _s3 varchar(32), _e3 varchar(32), _s4 varchar(32), _e4 varchar(32),
  7. _where varchar(386), _having varchar(386), _orderby varchar(64),
  8. _branch int, _storage int)
  9. SQL SECURITY INVOKER
  10. BEGIN
  11. Declare _sbal_date varchar(8);
  12. Declare _ebal_date varchar(8);
  13. Declare _sbet1 varchar(64);
  14. Declare _ebet1 varchar(64);
  15. Declare _sbet2 varchar(64);
  16. Declare _ebet2 varchar(64);
  17. Declare _sbet3 varchar(64);
  18. Declare _ebet3 varchar(64);
  19. if _is_first_date(_sdate) then
  20. set _sbal_date = '19000101';
  21. set _ebal_date = '19000101';
  22. else
  23. set _sbal_date = _sdate;
  24. set _ebal_date = _edate;
  25. end if;
  26. if _s1 != '' and _e1 != '' then
  27. set _sbet1 = _s1;
  28. set _ebet1 = _e1;
  29. else
  30. set _sbet1 = '!';
  31. set _ebet1 = '駾';
  32. end if;
  33. if _s2 != '' and _e2 != '' then
  34. set _sbet2 = _s2;
  35. set _ebet2 = _e2;
  36. else
  37. set _sbet2 = '!';
  38. set _ebet2 = '駾';
  39. end if;
  40. if _s3 != '' and _e3 != '' then
  41. set _sbet3 = _s3;
  42. set _ebet3 = _e3;
  43. else
  44. set _sbet3 = '!';
  45. set _ebet3 = '駾';
  46. end if;
  47. insert into dbt_list_sum
  48. ( created_on, list_token, h_id, id, c1,
  49. d1, d2, d3, d4,
  50. c2, c3, c4, order_by )
  51. select unix_timestamp(),'aaa', mx.id, mx.id, mx.company_name,
  52. bal_amt, 0, 0, 0,
  53. cgroup_name, cgroup_code, concat(company_name, company_no), concat(company_name, company_no)
  54. from
  55. ( select buyer_id, bal_amt
  56. from dbr_bal_reward
  57. where yyyy_mm = _prev_month(_sdate) and branch_id = _branch
  58. ) as last_mbal
  59. inner join dbr_company as mx on mx.id = last_mbal.buyer_id
  60. inner join dbr_cgroup as cgr on cgroup_id = cgr.id
  61. where cgr.cgroup_code between _sbet1 and _ebet1
  62. and mx.company_name between _sbet2 and _ebet2
  63. and ( company_class = 'AA' or company_class = 'AB' );
  64. insert into dbt_list_type1
  65. ( created_on, list_token, id, c1, d1, d2, d3, d4, c2, c3)
  66. select unix_timestamp(), _listToken, mx.id, company_name, bal.last_bal,
  67. ifnull(io_sum.in_tot, 0), ifnull(io_sum.out_tot, 0),
  68. bal.last_bal + ifnull(io_sum.in_tot, 0) - ifnull(io_sum.out_tot, 0) as curr_bal,
  69. cgroup_name, cgroup_code
  70. from
  71. dbr_company as mx
  72. inner join dbr_cgroup as cgr on mx.cgroup_id = cgr.id
  73. left join
  74. (
  75. select mxv.id,
  76. ifnull(last_mbal.bal_amt,0) + ifnull(crd_bal.crd_amt,0) as last_bal
  77. from dbr_company as mxv
  78. left join
  79. ( select buyer_id, bal_amt
  80. from dbr_bal_credit
  81. where yyyy_mm = _prev_month(_sdate) and branch_id = _branch
  82. ) as last_mbal
  83. on mxv.id = last_mbal.buyer_id
  84. left join
  85. ( select crd.buyer_id, sum(credit_amt*credit_status) AS crd_amt
  86. from dbr_credit_bd as crd
  87. inner join dbr_deal_type as deal on deal.id = crd.deal_type_id
  88. where avail_date between _first_date(_sbal_date) and _prev_date(_ebal_date)
  89. and branch_id = _branch
  90. group by crd.buyer_id
  91. ) as crd_bal
  92. on mxv.id = crd_bal.buyer_id
  93. ) bal
  94. on mx.id = bal.id
  95. left join
  96. ( select crd.buyer_id,
  97. sum(credit_amt*cast((credit_status+1)/2 as int)) as in_tot,
  98. sum(credit_amt*cast(-1*(credit_status-1)/2 as int)) as out_tot
  99. from dbr_credit_bd as crd
  100. inner join dbr_deal_type as deal on deal.id = crd.deal_type_id
  101. where avail_date between _sdate and _edate
  102. and branch_id = _branch
  103. group by crd.buyer_id
  104. ) as io_sum
  105. on mx.id = io_sum.buyer_id
  106. where cgr.cgroup_code between _sbet1 and _ebet1
  107. and mx.company_name between _sbet2 and _ebet2
  108. and mx.company_class = 'AA' or mx.company_class = 'AB'
  109. order by company_name asc;
  110. END;
  111. //
  112. DELIMITER ;