56,677
社区成员
发帖
与我相关
我的任务
分享
CREATE TABLE `finance_day_invoicing` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`reference_date` date DEFAULT '0000-00-00' COMMENT '日期',
`sku` varchar(64) DEFAULT NULL,
`warehouseid` int(10) DEFAULT '0' COMMENT '仓库Id',
`iqs` int(10) DEFAULT '0' COMMENT '期初数量',
`ics` decimal(20,5) DEFAULT '0.00000' COMMENT '期初成本',
`wq10` int(10) DEFAULT '0' COMMENT '采购入库数量',
`wc10` decimal(20,5) DEFAULT '0.00000' COMMENT '采购入库金额',
`dq10` int(10) DEFAULT '0' COMMENT '销售出库数量',
`dc10` decimal(20,5) DEFAULT '0.00000' COMMENT '销售出库成本',
`iqe` int(10) DEFAULT '0' COMMENT '期末库存数量',
`ice` decimal(20,5) DEFAULT '0.00000' COMMENT '期末库存成本',
PRIMARY KEY (`id`),
KEY `IDX_DATE` (`reference_date`),
KEY `IDX_SKU` (`sku`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8 COMMENT='日结销存报表';
CREATE TABLE `finance_day_invoicing` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`reference_date` date DEFAULT '0000-00-00' COMMENT '日期',
`sku` varchar(64) DEFAULT NULL,
`warehouseid` int(10) DEFAULT '0' COMMENT '仓库Id',
`iqs` int(10) DEFAULT '0' COMMENT '期初数量',
`ics` decimal(20,5) DEFAULT '0.00000' COMMENT '期初成本',
`wq10` int(10) DEFAULT '0' COMMENT '采购入库数量',
`wc10` decimal(20,5) DEFAULT '0.00000' COMMENT '采购入库金额',
`dq10` int(10) DEFAULT '0' COMMENT '销售出库数量',
`dc10` decimal(20,5) DEFAULT '0.00000' COMMENT '销售出库成本',
`iqe` int(10) DEFAULT '0' COMMENT '期末库存数量',
`ice` decimal(20,5) DEFAULT '0.00000' COMMENT '期末库存成本',
PRIMARY KEY (`id`),
KEY `IDX_DATE` (`reference_date`),
KEY `IDX_SKU` (`sku`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='日结销存报表';
INSERT INTO `finance_day_invoicing` VALUES ('1', '2018-01-09', 'ABC123', '1', '220', '12320.00000', '0', '0.00000', '26', '1456.25090', '0', '0.00000');
INSERT INTO `finance_day_invoicing` VALUES ('2', '2018-01-10', 'ABC123', '1', '0', '0.00000', '0', '0.00000', '3', '168.02895', '0', '0.00000');
INSERT INTO `finance_day_invoicing` VALUES ('3', '2018-01-11', 'ABC123', '1', '0', '0.00000', '0', '0.00000', '5', '280.04825', '0', '0.00000');
INSERT INTO `finance_day_invoicing` VALUES ('23', '2018-01-09', 'DEF123', '1', '100', '1500.00000', '0', '0.00000', '1', '15.56479', '0', '0.00000');
INSERT INTO `finance_day_invoicing` VALUES ('24', '2018-01-10', 'DEF123', '1', '0', '0.00000', '0', '0.00000', '1', '15.56479', '0', '0.00000');
UPDATE finance_day_invoicing a, (
SELECT id,
@qs:=if(@sku<=>sku and @warehouseid<=>warehouseid and @reference_date<reference_date, @qs, iqs) as iqs,
@cs:=if(@sku<=>sku and @warehouseid<=>warehouseid and @reference_date<reference_date, @cs, ics) as ics,
@qs:=@qs+wq10-dq10 as iqe,
@cs:=@cs+wc10-dc10 as ice,
@sku:=sku, @warehouseid:=warehouseid, @reference_date:=reference_date
FROM finance_day_invoicing
,( SELECT @sku:=null, @warehouseid:=null, @reference_date:=null ) _r
ORDER BY sku, warehouseid, reference_date
) b
set a.iqs=b.iqs, a.ics=b.ics, a.iqe=b.iqe, a.ice=b.ice
where a.id=b.id
;