z-tmp-집계표.sql 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. drop procedure if exists fnc_curr_item_bal;
  2. DELIMITER //
  3. create procedure fnc_curr_item_bal( _curr_date varchar(8), _storage int, _target int)
  4. SQL SECURITY INVOKER
  5. BEGIN
  6. Declare _curr_bal decimal(20,4) default "0.0000";
  7. Declare _curr_bad decimal(20,4) default "0.0000";
  8. Declare _sum_stkio decimal(20,4) default "0.0000";
  9. Declare _sum_bad decimal(20,4) default "0.0000";
  10. Declare _bal_date varchar(8);
  11. if _is_first_date(_curr_date) then
  12. set _bal_date = '19000101';
  13. else
  14. set _bal_date = _curr_date;
  15. end if;
  16. select bal_qty, bad_bal_qty
  17. into _curr_bal, _curr_bad
  18. from dbr_bal_item
  19. where item_id = _target
  20. and yyyy_mm = _prev_month(_curr_date) and storage_id = _storage;
  21. select sum(io_qty*stock_status), sum(io_qty*badstk_status)
  22. into _sum_stkio, _sum_bad
  23. from ( select item_id, io_qty, stock_status, badstk_status
  24. from dbr_stock_io as stk
  25. inner join dbr_deal_type as deal on deal.id = stk.deal_type_id
  26. where item_id = _target
  27. and io_date between _first_date(_bal_date) and _bal_date
  28. and storage_id = _storage
  29. ) stkio
  30. group by stkio.item_id;
  31. select _curr_bal + _sum_stkio as c1, _curr_bad + _sum_bad as c2;
  32. END;
  33. //
  34. DELIMITER ;
  35. call fnc_curr_item_bal('20230228', 1, 1) ;
  36. ALTER TABLE `crm_account` CHANGE `account_name` `order_by` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '';
  37. ALTER TABLE `crm_account_item` CHANGE `item_name` `order_by` VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '';
  38. select bal_amt
  39. from dbr_bal_reward
  40. where buyer_id = 10
  41. and yyyy_mm = _prev_month('20230228') and branch_id = 1;
  42. select sum(reward_amt*reward_status)
  43. from dbr_reward_bd as rwd
  44. inner join dbr_deal_type as deal on deal.id = deal_type_id
  45. where buyer_id = 10
  46. and avail_date between '20230201' and '20230228'
  47. and branch_id = 1
  48. group by buyer_id;