请帮助我如何创建 mysql 查询以用于 fifo 方法中的报告,
表 persediaan_source
| ID | id_barang | 朱姆拉 | 哈尔加 | 唐加尔 | 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
Run Code Online (Sandbox Code Playgroud)
我需要像下面这样的报告
| ID | 日期 | 评论 | 购买数量 | 购买价格 | 购买总数 | 使用数量 | 使用数量详细信息 | 使用价格 | 使用总数 | 巴尔数量 | bal_qty_detail | 价格 | bal_total |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2022-07-15 05:55:23 | 买 | 12 | 1050000 | 12600000 | 0 | 0 | 0 | 0 | 12 | 12 | 1050000 | 12600000 |
| 2 | 2022-07-15 05:55:52 | 使用 | 0 | 0 | 0 | 8 | 8 | 1050000 | 8400000 | 4 | 4 | 1050000 | 4200000 |
| 3 | 2022-07-15 05:56:22 | 买 | 16 | 1100000 | 17600000 | 0 | 0 | 0 | 0 | 20 | 4 | 1050000 | 4200000 |
| 4 | 2022-07-15 05:56:22 | 买 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 16 | 1100000 | 17600000 |
| 5 | 2022-07-15 05:59:09 | 使用 | 0 | 0 | 0 | 10 | 4 | 1050000 | 4200000 | 10 | 0 | 1050000 | 0 |
| 6 | 2022-07-15 05:59:09 | 使用 | 0 | 0 | 0 | 0 | 6 | 1100000 | 6600000 | 0 | 10 | 1100000 | 11000000 |
第 3 行中必须在bal_qty_detail中进行细分,因为价格不同并且与之前的价格相比还有剩余数量,同样在第 5 行中也必须在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);
Run Code Online (Sandbox Code Playgroud)
您好,使用以下过程您可以实现提及报告格式。但要处理 fifo 的粒度,您应该避免在 mysql 中创建这些逻辑。
SELECT @bal_qty:=0,@old_qty:=0,@old_qty_price:=0,@new_qty:=0,@new_qty_price:=0;
CREATE TEMPORARY TABLE test1
SELECT id, tanggal as date,
@remarks:=if(id_jenis_transaksi=1,'Buy','Use') as remarks,
@buy_qty:=if(id_jenis_transaksi=1,jumlah,0) as buy_qty,
@buy_price:=if(id_jenis_transaksi=1,harga,0) as buy_price,
@buy_total:=(@buy_qty * @buy_price) as buy_total,
@use_qty:=if(id_jenis_transaksi=2,jumlah,0) as use_qty,
@use_qty_detail:=if(@remarks='Use',if(@old_qty>0,@old_qty, @use_qty),0) as use_qty_detail,
@use_price:=@old_qty_price as use_price,
@use_total:=(@use_qty * @use_price) as use_total,
@bal_qty:=if(@remarks='Buy',@bal_qty + @buy_qty, if(@bal_qty>@use_qty,@bal_qty - @use_qty, @bal_qty)) as bal_qty,
@old_qty:=if(@old_qty > 0, @old_qty, if(@remarks='Use',@bal_qty, @old_qty)) as old_qty,
@old_qty_price:=if(@old_qty_price > 0, @old_qty_price, @buy_price) as old_qty_price,
@new_qty:=if(@old_qty>0,@buy_qty, if(@new_qty>0, @new_qty,0)) as new_qty,
@new_qty_price:=if(@old_qty_price > 0 and @buy_price > 0, @buy_price, if(@new_qty_price>0,@new_qty_price,0)) as new_qty_price,
@bal_qty_detail:=if(@old_qty > 0, @old_qty, @buy_qty) as bal_qty_detail,
@bal_price:=@old_qty_price as bal_price,
@bal_total:=(@bal_qty_detail* @old_qty_price) as bal_total,
@split_flag:=if(@bal_qty != @buy_qty and @bal_qty != @old_qty,1,0) as split_flag
FROM persediaan_source;
CREATE TEMPORARY TABLE test2 select * from test1 where split_flag=1
SELECT @cnt:=0;
SELECT (@cnt:=@cnt + 1) as 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
FROM (
(SELECT * FROM test1)
UNION ALL
(SELECT id, date, remarks, 0 as buy_qty, 0 as buy_price, 0 as buy_total, 0 as use_qty,
@used_qty:=(use_qty - use_qty_detail) as use_qty_detail,
if(remarks='Use',@new_qty_price,0) as use_price,
(@used_qty * new_qty_price) as use_total, 0 as bal_qty, 0 as old_qty,
0 as old_qty_price, 0 as new_qty,0 as new_qty_price,
@bal_qty_detail:=if(remarks='Buy', buy_qty, bal_qty) as bal_qty_detail,
new_qty_price as bal_price,
(@bal_qty_detail * new_qty_price) as bal_total, split_flag
FROM test2)
) as t order by 2
Run Code Online (Sandbox Code Playgroud)