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

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