Advertisement
cdsatrian

query stock opname

Oct 18th, 2013
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.16 KB | None | 0 0
  1. SELECT zz.barcode, zz.qty_sisa AS qty, zz.toko
  2. FROM (
  3.   SELECT msk.barcode AS barcode, msk.qty-IFNULL(klr.qty, 0) AS qty_sisa, msk.toko
  4.   FROM
  5.   (
  6.     SELECT a.barcode,SUM(a.qty) AS qty, a.toko
  7.     FROM
  8.     (
  9.         SELECT barcode, SUM(qty) AS qty, toko
  10.         FROM nifn_saldo_awal_pos
  11.         WHERE
  12.           toko = 'HM2C'
  13.         GROUP BY barcode
  14.       UNION
  15.         SELECT a.barcode,SUM(a.qty) AS qty, b.toko
  16.         FROM nifn_trans_klr_barang_detail AS a
  17.          JOIN nifn_trans_klr_barang_master AS b ON a.id_transaksi = b.id_masuk
  18.          WHERE
  19.            b.toko = 'HM2C' AND a.validated = 1
  20.          GROUP BY a.barcode
  21.       UNION
  22.         SELECT a.barcode, SUM(a.qty) AS qty, b.tujuan AS toko
  23.         FROM nifn_trans_mutasi_pos_detail AS a
  24.           JOIN nifn_trans_mutasi_pos_master AS b ON a.id_transaksi = b.id_masuk
  25.         WHERE
  26.           b.tujuan = 'HM2C'  AND a.validated = 1 AND b.validated = 1
  27.         GROUP BY
  28.           a.barcode
  29.       UNION
  30.         SELECT a.barcode, SUM(a.qty) AS qty, b.id_cabang AS toko
  31.         FROM nifn_trans_exchange_detail AS a
  32.         JOIN nifn_trans_exchange_master AS b ON a.id_transaksi = b.id_transaksi
  33.         WHERE
  34.          b.id_cabang = 'HM2C' GROUP BY a.barcode
  35.     ) AS a
  36.     GROUP BY barcode
  37.   ) AS msk
  38.   LEFT JOIN
  39.   (
  40.     SELECT a.barcode, SUM(a.qty) AS qty
  41.     FROM
  42.     (
  43.         SELECT a.barcode, SUM(a.qty) AS qty
  44.         FROM nifn_trans_pos_detail AS a
  45.           JOIN nifn_trans_pos_master AS b ON a.id_transaksi = b.pos_id_transaksi
  46.         WHERE b.pos_toko = 'HM2C'
  47.         GROUP BY a.barcode
  48.       UNION
  49.         SELECT a.barcode, SUM(a.qty) AS qty
  50.         FROM nifn_trans_mutasi_pos_detail AS a
  51.           JOIN nifn_trans_mutasi_pos_master AS b ON a.id_transaksi = b.id_masuk
  52.         WHERE b.dari = 'HM2C'
  53.         GROUP BY a.barcode
  54.       UNION
  55.         SELECT a.barcode, SUM(a.qty) AS qty
  56.         FROM nifn_trans_retur_gudang_detail AS a
  57.           JOIN nifn_trans_retur_gudang_master AS b ON a.id_transaksi = b.id_retur
  58.         WHERE b.toko = 'HM2C'
  59.         GROUP BY a.barcode
  60.     ) AS a GROUP BY barcode
  61.   ) AS klr ON msk.barcode = klr.barcode
  62. ) AS zz
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement