我有一個包含股票的交易表:SQL查詢來顯示更改爲「總計」列
+------+----------------------+------------------+
| Item | Running Stock Total | Transaction Time |
+------+----------------------+------------------+
| foo | 4 | 2012-05-12 11:07 |
| bar | 3 | 2012-05-12 10:42 |
| bar | 3 | 2012-05-12 9:42 |
| bar | 2 | 2012-05-11 15:42 |
| foo | 3 | 2012-05-11 10:02 |
| bar | 3 | 2012-05-10 13:44 |
...etc...
+------+----------------------+------------------+
即任何時候有事股票,排在該表 - 創建,這可能意味着股票水平上漲(訂購新股票),下跌(股票賣出)或保持不變(股票重新定位)。
我需要創建一個sql查詢,該查詢僅返回庫存級別實際上已更改爲某個特定部分的行,並且需要顯示「庫存增加」和「庫存減少」列中的更改。
即1 Item='bar'
+------+-----------+------------+----------------------+------------------+
| Item | Stock Up | Stock Down | Running Stock Total | Transaction Time |
+------+-----------+------------+----------------------+------------------+
| bar | 1 | 0 | 3 | 2012-05-12 9:42 |
| bar | 0 | 1 | 2 | 2012-05-11 15:42 |
| bar | 1 | 0 | 3 | 2012-05-10 13:44 |
+------+-----------+------------+----------------------+------------------+
e.g.2 Item='foo'
+------+-----------+------------+----------------------+------------------+
| Item | Stock Up | Stock Down | Running Stock Total | Transaction Time |
+------+-----------+------------+----------------------+------------------+
| foo | 1 | 0 | 4 | 2012-05-12 11:07 |
| foo | 2 | 0 | 3 | 2012-05-11 10:02 |
+------+-----------+------------+----------------------+------------------+
所以像...
SELECT
Item, {xyz} as 'Stock Up', {abc} as 'Stock Down', `Running Stock Total`, `Transaction Time`
FROM
`StockTransactions`
WHERE
`Item`='foo'
HAVING
('Stock Up'>0 or 'Stock Down'>0)
可以這樣做?
如何區分股票何時上漲以及股票何時下跌? – JHS
通過使用「Running Stock Total」 – Urbycoz
您的語法看起來像SQL Server,但您的問題被標記爲MySQL。你在用哪個? – eggyal