如下使用CASE()和Modulus,假定Batch
總是由1 inceremnted和Doc_Type
始終這兩個值AA
並以相同的順序BB
:
CREATE TABLE Data (
Doc_Type VARCHAR(10),
Batch INT,
Qty DECIMAL(4,2)
);
INSERT INTO Data VALUES
('AA', 1, 20.5),
('BB', 2, 10 ),
('AA', 3, 6 ),
('BB', 4, 7 ),
('AA', 5, 8 );
SELECT D.Doc_Type, D.Batch,
CASE WHEN D.Batch % 2 = 0 Then 0 ELSE D.Qty END AS Soaking_In,
CASE WHEN D.Batch % 2 = 1 Then 0 ELSE D.Qty END AS Soaking_Out
FROM Data D;
個結果:
+----------+-------+------------+-------------+
| Doc_Type | Batch | Soaking_In | Soaking_Out |
+----------+-------+------------+-------------+
| AA | 1 | 20,50 | 0,00 |
| BB | 2 | 0,00 | 10,00 |
| AA | 3 | 6,00 | 0,00 |
| BB | 4 | 0,00 | 7,00 |
| AA | 5 | 8,00 | 0,00 |
+----------+-------+------------+-------------+
Demo
您使用的兩個表Transaction_Hdr和Transaction_dtl? –