嗯,我有一個我無法解決的思維劃痕。 總新手:) 我需要計算庫存項目的數量和檢測負值,如果他們出現在計算:SQL數量計算
inquantity | outquantity
100 | 0
10 | 0
0 | 50
0 | 100
20 | 0
0 | 80
15 | 0
100 | 0
,我需要計算Quty:
inquantity | outquantity | Quty
100 | 0 | 100
10 | 0 | 110
0 | 50 | 60
0 | 100 | -40
20 | 0 | -20
0 | 80 | -100
15 | 0 | -85
100 | 0 | 15
我該怎麼辦呢?
關於Abhik的帖子:
select
id ,
inquantity,
outquantity,
@qty:= (@qty+inquantity)-outquantity as qty
from quantity,(select @qty:= 0)r
order by id;
有重置上的productid變化的變量@qty的可能性?
+----+-----------+------------+-------------+------+
| id | productid | inquantity | outquantity | qty |
+----+-----------+------------+-------------+------+
| 1 | 1 | 100 | 0 | 100 |
| 2 | 1 | 10 | 0 | 110 |
| 3 | 1 | 0 | 50 | 60 |
| 4 | 1 | 0 | 100 | -40 |
| 5 | 2 | 20 | 0 | 20 |
| 6 | 2 | 0 | 80 | -60 |
| 7 | 2 | 15 | 0 | -45 |
| 8 | 3 | 100 | 0 | 100 |
+----+-----------+------------+-------------+------+
你不能,與數據結構就像它一樣。 SQL表格表示*無序*集合。沒有固有的順序,除非你有一個指定順序的列。你有這個信息的日期或ID嗎? –
是的,我有產品ID和這已經是過濾表的輸出:SELECT di.inquantity, di.outquantity FROM docfinitem AS二 WHERE di.warehouseid = 7 AND di.productid = 4563 – Nick
包括產品ID在你的例子中! (但是這還不夠......你如何知道沒有時間戳或交易號碼或類似的訂單?) – jarlh