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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. truncate table dbt_list_sum;
  2. truncate table dbt_list_type1;
  3. drop procedure if exists lt1_credit_bd_sum_bal;
  4. DELIMITER //
  5. create procedure lt1_credit_bd_sum_bal( _listToken varchar(21), _sdate varchar(8), _edate varchar(8),
  6. _s1 varchar(32), _e1 varchar(32), _s2 varchar(32), _e2 varchar(32),
  7. _s3 varchar(32), _e3 varchar(32), _s4 varchar(32), _e4 varchar(32),
  8. _where varchar(386), _having varchar(386), _orderby varchar(64),
  9. _branch int, _storage int)
  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(), _listToken, mx.id, mx.id, mx.company_name,
  52. bal_amt, 0, 0, 0,
  53. cgroup_name, cgroup_code, concat(full_name, ' ', company_no), concat(company_name, ' ', company_no)
  54. from
  55. ( select buyer_id, bal_amt
  56. from dbr_bal_credit
  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. -- 적립금 합계 --
  65. insert into dbt_list_sum
  66. ( created_on, list_token, h_id, id, c1,
  67. d1, d2, d3, d4,
  68. c2, c3, c4, order_by )
  69. select unix_timestamp(), _listToken, cmp.id, cmp.id, company_name,
  70. 0, sum(credit_amt*cast((credit_status+1)/2 as int)), sum(credit_amt*cast(-1*(credit_status-1)/2 as int)), sum(credit_amt*credit_status),
  71. cgroup_name, cgroup_code, concat(full_name, ' ', company_no), concat(company_name, ' ', company_no)
  72. from
  73. ( select id, buyer_id, branch_id, deal_type_id, credit_amt
  74. from dbr_credit_bd
  75. where avail_date between _sdate and _edate
  76. ) as sbbd
  77. inner join dbr_deal_type as deal on deal.id = sbbd.deal_type_id
  78. inner join ( select cmp.id, company_name, full_name, company_no, cgroup_name, cgroup_code
  79. from dbr_company as cmp
  80. inner join dbr_cgroup as cgr on cgroup_id = cgr.id
  81. where cgr.cgroup_code between _sbet1 and _ebet1
  82. and cmp.company_name between _sbet2 and _ebet2
  83. and ( company_class = 'AA' or company_class = 'AB' )
  84. ) as cmp on cmp.id = sbbd.buyer_id
  85. where branch_id = _branch
  86. group by cmp.id;
  87. -- dbt_list_type1 으로 집계 --
  88. insert into dbt_list_type1
  89. ( created_on, list_token, h_id, id, c1,
  90. d1, d2, d3, d4,
  91. c2, c3, c4, order_by)
  92. select min(unix_timestamp()), min(_listToken), min(id), min(id), min(c1),
  93. sum(d1),
  94. sum(d2),
  95. sum(d3),
  96. sum(d1) + sum(d2) - sum(d3),
  97. min(c2), min(c3), min(c4), min(order_by)
  98. from dbt_list_sum as mx
  99. where list_token = _listToken
  100. group by mx.id
  101. having min(mx.id) is not null
  102. order by order_by asc;
  103. END;
  104. //
  105. DELIMITER ;