0
            tablename: lineitem
            +----------------+-----+-----------+--------+
            | item           | amt | allocated | lotnum |
            +----------------+-----+-----------+--------+
            | bags galore    |  20 | Received  | 000590 |
            | test inventory |  10 | Received  |        |
            | bags galore    |  19 | Shipped   | 000590 |
            | test inventory |  20 | Received  |        |
            +----------------+-----+-----------+--------+

你好,我正在尝试创建一个 SQL 公式,但我碰壁了。上面是我的桌子。

我正在寻找一个公式,该公式将比较项目并从匹配项目和 lotnum 的 Received 中减去它的发货位置,并将 Received 加在一起。

所以最终的结果是这样的。

            +----------------+-----+--------+
            | item           | amt | lotnum |
            +----------------+-----+--------+
            | bags galore    |  1  | 000590 |
            +----------------+-----+--------+
            | test inventory |  30 |        |
            +----------------+-----+--------+
4

2 回答 2

3
SELECT
    item,
    SUM(IF(allocated='Received',amt,-amt)) as amt,
    lotnum
FROM lineitem
GROUP BY item, lotnum 
于 2012-07-27T18:49:01.417 回答
0

也许是这样的?

select item,
sum(case allocated when 'Received' then amt when 'Shipped' then -amt end) as amt,
lotnum
from lineitem
group by item, lotnum
于 2012-07-27T18:49:43.323 回答