是这样的
select front_num from sr_jxc_inventory_log
where add_time>='2017-11-01 00:00:00'
查询出的数据是
5
6
15
13
我想把这个数据作为 期初数据(cqjc) 插入到下面的语句中
select L.goods_id,L.warehouse_id, G.model, G.factory,G.barcode, W.name,
sum(L.actual_number) jc,
sum(case when L.actual_number> 0 then L.actual_number else 0 end) as sr,
sum(case when L.actual_number <0 then L.actual_number else 0 end ) as zc,
(select front_num from `sr_jxc_inventory_log`
where warehouse_id='1' and
add_time>'2017-11-02 00:00:00'
order by add_time
limit 1)as cqjc,
(SUM(L.actual_number)+(select front_num from `sr_jxc_inventory_log`
where warehouse_id='1' and
add_time>'2017-11-02 00:00:00'
order by add_time
limit 1)
) as qmjc
FROM sr_jxc_inventory_log as L
LEFT JOIN sr_goods as G ON G.goods_id = L.goods_id
LEFT JOIN sr_jxc_ware as W ON W.wareid = L.warehouse_id
where L.warehouse_id='1' and L.add_time>'2017-11-02 00:00:00'
group by L.goods_id;
这里面的cqjc 查询后都显示为5
#9
select L.goods_id,L.warehouse_id, G.model, G.factory,G.barcode, W.name, sum(L.actual_number) jc, sum(case when L.actual_number> 0 then L.actual_number else 0 end) as sr, sum(case when L.actual_number <0 then L.actual_number else 0 end ) as zc, B.front_num as cqjc, (SUM(L.actual_number)+(select front_num from `sr_jxc_inventory_log` where warehouse_id='1' and add_time>'2017-11-02 00:00:00' order by add_time limit 1) ) as qmjc FROM sr_jxc_inventory_log as L LEFT JOIN sr_goods as G ON G.goods_id = L.goods_id LEFT JOIN sr_jxc_ware as W ON W.wareid = L.warehouse_id LEFT JOIN (select front_num,goods_id from `sr_jxc_inventory_log` as A where warehouse_id='1' and add_time>'2017-11-02 00:00:00' and not exists (select 1 from `sr_jxc_inventory_log` where goods_id=A.goods_id and add_time<A.add_time and add_time>'2017-11-02 00:00:00' )) as B ON L.goods_id=B.goods_id where L.warehouse_id='1' and L.add_time>'2017-11-02 00:00:00' group by L.goods_id;
是这样的
select front_num from sr_jxc_inventory_log
where add_time>='2017-11-01 00:00:00'
查询出的数据是
5
6
15
13
我想把这个数据作为 期初数据(cqjc) 插入到下面的语句中
select L.goods_id,L.warehouse_id, G.model, G.factory,G.barcode, W.name,
sum(L.actual_number) jc,
sum(case when L.actual_number> 0 then L.actual_number else 0 end) as sr,
sum(case when L.actual_number <0 then L.actual_number else 0 end ) as zc,
(select front_num from `sr_jxc_inventory_log`
where warehouse_id='1' and
add_time>'2017-11-02 00:00:00'
order by add_time
limit 1)as cqjc,
(SUM(L.actual_number)+(select front_num from `sr_jxc_inventory_log`
where warehouse_id='1' and
add_time>'2017-11-02 00:00:00'
order by add_time
limit 1)
) as qmjc
FROM sr_jxc_inventory_log as L
LEFT JOIN sr_goods as G ON G.goods_id = L.goods_id
LEFT JOIN sr_jxc_ware as W ON W.wareid = L.warehouse_id
where L.warehouse_id='1' and L.add_time>'2017-11-02 00:00:00'
group by L.goods_id;
这里面的cqjc 查询后都显示为5
#9
select L.goods_id,L.warehouse_id, G.model, G.factory,G.barcode, W.name, sum(L.actual_number) jc, sum(case when L.actual_number> 0 then L.actual_number else 0 end) as sr, sum(case when L.actual_number <0 then L.actual_number else 0 end ) as zc, B.front_num as cqjc, (SUM(L.actual_number)+(select front_num from `sr_jxc_inventory_log` where warehouse_id='1' and add_time>'2017-11-02 00:00:00' order by add_time limit 1) ) as qmjc FROM sr_jxc_inventory_log as L LEFT JOIN sr_goods as G ON G.goods_id = L.goods_id LEFT JOIN sr_jxc_ware as W ON W.wareid = L.warehouse_id LEFT JOIN (select front_num,goods_id from `sr_jxc_inventory_log` as A where warehouse_id='1' and add_time>'2017-11-02 00:00:00' and not exists (select 1 from `sr_jxc_inventory_log` where goods_id=A.goods_id and add_time<A.add_time and add_time>'2017-11-02 00:00:00' )) as B ON L.goods_id=B.goods_id where L.warehouse_id='1' and L.add_time>'2017-11-02 00:00:00' group by L.goods_id;