简体   繁体   中英

Need mysql query FIFO

please help me how to create mysql query for reporting in fifo method,

table persediaan_source

id id_barang jumlah harga tanggal id_jenis_transaksi
89 26 12 1050000 2022-07-15 05:55:23 1
90 26 8 0 2022-07-15 05:55:52 2
91 26 16 1100000 2022-07-15 05:56:22 1
95 26 10 0 2022-07-15 05:59:09 2
id_jenis_transaksi = 1 is Buy
id_jenis_transaksi = 2 is Use

i need report like this below

id date remarks buy_qty buy_price buy_total use_qty use_qty_detail use_price use_total bal_qty bal_qty_detail bal_price bal_total
1 2022-07-15 05:55:23 Buy 12 1050000 12600000 0 0 0 0 12 12 1050000 12600000
2 2022-07-15 05:55:52 Use 0 0 0 8 8 1050000 8400000 4 4 1050000 4200000
3 2022-07-15 05:56:22 Buy 16 1100000 17600000 0 0 0 0 20 4 1050000 4200000
4 2022-07-15 05:56:22 Buy 0 0 0 0 0 0 0 0 16 1100000 17600000
5 2022-07-15 05:59:09 Use 0 0 0 10 4 1050000 4200000 10 0 1050000 0
6 2022-07-15 05:59:09 Use 0 0 0 0 6 1100000 6600000 0 10 1100000 11000000

in row #3 must be breakdown in bal_qty_detail because there is a different price and have a remaining qty from a previous price, also in row #5 must be breakdown in use_qty_detail

CREATE TABLE `persediaan_source` (
  `id` int(11) NOT NULL,
  `id_barang` int(11) NOT NULL,
  `jumlah` double NOT NULL,
  `harga` double NOT NULL,
  `tanggal` datetime NOT NULL,
  `id_jenis_transaksi` tinyint(4) NOT NULL COMMENT 'id = 1 -> buy, id = 2 -> use'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `persediaan_source` (`id`, `id_barang`, `jumlah`, `harga`, `tanggal`, `id_jenis_transaksi`) VALUES
(89, 26, 12, 1050000, '2022-07-15 05:55:23', 1),
(90, 26, 8, 0, '2022-07-15 05:55:52', 2),
(91, 26, 16, 1100000, '2022-07-15 05:56:22', 1),
(95, 26, 10, 0, '2022-07-15 05:59:09', 2);

Based on you required logic create another select query in this case you need to do in case of buy if u already have bal qty and union both the query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM