Advertisement
henikseptiana15

Cek Stock

Dec 18th, 2017
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WITH data_log AS(
  2.         -- Ambil nilai log stock per periode bulan selanjutnya setelah tutup bulan
  3.     SELECT tenant_id, warehouse_id, product_id, product_balance_id, SUM(qty) AS qty
  4.     FROM in_log_product_balance_stock A
  5.     WHERE tenant_id = 10
  6.         AND SUBSTRING(doc_date, 1, 6) = '201711'
  7.     GROUP BY tenant_id, warehouse_id, product_id, product_balance_id
  8.     UNION ALL
  9.     -- Ambil nilai saldo awal per nextYearMonthAfterClosing dan doc_type_id = -99
  10.     SELECT tenant_id, warehouse_id, product_id, product_balance_id, SUM(qty) AS qty
  11.     FROM in_summary_monthly_qty
  12.     WHERE tenant_id = 10
  13.         AND date_year_month = '201711'
  14.         AND doc_type_id = -99
  15.     GROUP BY tenant_id, warehouse_id, product_id, product_balance_id, product_status
  16.     ORDER BY product_balance_id
  17.    
  18. ), data_summary_log AS(
  19.     -- Summary nilai saldo awal dan log stock
  20.     SELECT tenant_id, warehouse_id, product_id, product_balance_id, SUM(qty) AS qty
  21.     FROM data_log
  22.     GROUP BY tenant_id, warehouse_id, product_id, product_balance_id
  23.     ORDER BY product_balance_id, product_id
  24.    
  25. ), data_current_stock AS (
  26.     SELECT tenant_id, warehouse_id, product_id, product_balance_id, SUM(qty) AS qty
  27.     FROM in_product_balance_stock
  28.     GROUP BY tenant_id, warehouse_id, product_id, product_balance_id
  29.     ORDER BY product_balance_id, product_id
  30. )
  31.     -- Bandingkan (log_stock + saldo_awal) dengan current stock
  32.     SELECT A.tenant_id, A.warehouse_id, A.product_id, A.product_balance_id, A.qty, B.qty AS current_stock
  33.     FROM data_summary_log A
  34.     INNER JOIN data_current_stock B   ON A.tenant_id = B.tenant_id
  35.                         AND A.product_id = B.product_id
  36.                         AND A.product_balance_id = B.product_balance_id
  37.                         AND A.warehouse_id = B.warehouse_id
  38.     WHERE A.qty <> B.qty
  39.     ORDER BY A.product_balance_id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement