item | date | q_in | q_out |
----------------------------------
a | 25-08-2016 | 100 | 50
b | 26-09-2016 | 100 | 0
----- upto
b | 10-09-2016 | 0 | 100
我需要一個像這樣的輸出: -
item | open_stock | inward | outward | balance|
-----|------------|--------|---------|--------|
a | 1500 | 10000 | 500 | 1100 |
b | 500 | 5000 | 1000 | 4500 |
其中
- open_stock = q_in - q_out的餘額直至31-08-2016
- 轉入=總計q_in from 2016年1月9日向2016年10月9日
- 從2016年1月9日至2016年10月9日
- 餘額= open_stock +向內向外=總Q_OUT - 向外
和我的查詢是如下這需要你的幫助
select
item,
(select
(SUM(q_in) - SUM(q_out))
from
sale_table
where
date >= '2016-08-25' and date <= '2016-08-31') as open_stock,
SUM(q_in) as inward,
SUM(q_out) as outward ,
(***open_stock + inward - outward***) as balance
from
sale_table
where
date >= '2016-09-01' and date <= '2016-09-10'
group by
item
你期望的輸出不顯示任何數據,從而很難搞清楚w ^你正試圖做的帽子。 –
哪個RDBMS適合這個?請添加一個標籤來指定您是使用'mysql','postgresql','sql-server','oracle'還是'db2' - 或者其他的東西。 –
爲mysql添加了一個解決方案。 – Samar