z-tmp-집계표.sql 1.8 KB

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