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 式を作成しようとしていますが、壁にぶち当たりました。上は私のテーブルです。

アイテムを比較し、一致するアイテムとロットナムで受領から出荷された場所を差し引き、受領を一緒に追加する式を探しています。

というわけで最終結果はこれ。

            +----------------+-----+--------+
            | 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 に答える