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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. truncate table dbt_list_sum;
  2. truncate table dbt_list_type1;
  3. drop procedure if exists lt1_reward_bd_sum_bal;
  4. DELIMITER //
  5. create procedure lt1_reward_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. SQL SECURITY INVOKER
  11. BEGIN
  12. Declare _sbal_date varchar(8);
  13. Declare _ebal_date varchar(8);
  14. Declare _sbet1 varchar(64);
  15. Declare _ebet1 varchar(64);
  16. Declare _sbet2 varchar(64);
  17. Declare _ebet2 varchar(64);
  18. Declare _sbet3 varchar(64);
  19. Declare _ebet3 varchar(64);
  20. if _is_first_date(_sdate) then
  21. set _sbal_date = '19000101';
  22. set _ebal_date = '19000101';
  23. else
  24. set _sbal_date = _sdate;
  25. set _ebal_date = _edate;
  26. end if;
  27. if _s1 != '' and _e1 != '' then
  28. set _sbet1 = _s1;
  29. set _ebet1 = _e1;
  30. else
  31. set _sbet1 = '!';
  32. set _ebet1 = '힣';
  33. end if;
  34. if _s2 != '' and _e2 != '' then
  35. set _sbet2 = _s2;
  36. set _ebet2 = _e2;
  37. else
  38. set _sbet2 = '!';
  39. set _ebet2 = '힣';
  40. end if;
  41. if _s3 != '' and _e3 != '' then
  42. set _sbet3 = _s3;
  43. set _ebet3 = _e3;
  44. else
  45. set _sbet3 = '!';
  46. set _ebet3 = '힣';
  47. end if;
  48. insert into dbt_list_sum
  49. ( created_on, list_token, h_id, id, c1,
  50. d1, d2, d3, d4,
  51. c2, c3, c4, order_by )
  52. select unix_timestamp(), _listToken, mx.id, mx.id, mx.company_name,
  53. bal_amt, 0, 0, 0,
  54. cgroup_name, cgroup_code, concat(full_name, ' ', company_no), concat(company_name, ' ', company_no)
  55. from
  56. ( select buyer_id, bal_amt
  57. from dbr_bal_reward
  58. where yyyy_mm = _prev_month(_sdate) and branch_id = _branch
  59. ) as last_mbal
  60. inner join dbr_company as mx on mx.id = last_mbal.buyer_id
  61. inner join dbr_cgroup as cgr on cgroup_id = cgr.id
  62. where cgr.cgroup_code between _sbet1 and _ebet1
  63. and mx.company_name between _sbet2 and _ebet2
  64. and ( company_class = 'AA' or company_class = 'AB' );
  65. -- 적립금 합계 --
  66. insert into dbt_list_sum
  67. ( created_on, list_token, h_id, id, c1,
  68. d1, d2, d3, d4,
  69. c2, c3, c4, order_by )
  70. select unix_timestamp(), _listToken, cmp.id, cmp.id, company_name,
  71. 0, sum(reward_amt*cast((reward_status+1)/2 as int)), sum(reward_amt*cast(-1*(reward_status-1)/2 as int)), sum(reward_amt*reward_status),
  72. cgroup_name, cgroup_code, concat(full_name, ' ', company_no), concat(company_name, ' ', company_no)
  73. from
  74. ( select id, buyer_id, branch_id, deal_type_id, reward_amt
  75. from dbr_reward_bd
  76. where avail_date between _sdate and _edate
  77. ) as sbbd
  78. inner join dbr_deal_type as deal on deal.id = sbbd.deal_type_id
  79. inner join ( select cmp.id, company_name, full_name, company_no, cgroup_name, cgroup_code
  80. from dbr_company as cmp
  81. inner join dbr_cgroup as cgr on cgroup_id = cgr.id
  82. where cgr.cgroup_code between _sbet1 and _ebet1
  83. and cmp.company_name between _sbet2 and _ebet2
  84. and ( company_class = 'AA' or company_class = 'AB' )
  85. ) as cmp on cmp.id = sbbd.buyer_id
  86. where branch_id = _branch
  87. group by cmp.id;
  88. -- dbt_list_type1 으로 집계 --
  89. insert into dbt_list_type1
  90. ( created_on, list_token, h_id, id, c1,
  91. d1, d2, d3, d4,
  92. c2, c3, c4, order_by)
  93. select min(unix_timestamp()), min(_listToken), min(id), min(id), min(c1),
  94. sum(d1),
  95. sum(d2),
  96. sum(d3),
  97. sum(d1) + sum(d2) - sum(d3),
  98. min(c2), min(c3), min(c4), min(order_by)
  99. from dbt_list_sum as mx
  100. where list_token = _listToken
  101. group by mx.id
  102. having min(mx.id) is not null
  103. order by order_by asc;
  104. END;
  105. //
  106. DELIMITER ;