SQL:ADD& MINUS基于具有GROUP BY条件的Field类型

时间:2016-06-14 08:55:44

标签: mysql sql

此问题是升级版本here。 Giorgos Betsos先生提供的解决方案工作正常。但是我将我的要求升级为按PRODUCT_ID过滤(我假设使用组)。新表现在看起来像这样

The XML data type cannot be compared or sorted, except when using the IS NULL operator.

SQL FIDDLE

如何根据 PRODUCT_ID 添加/减去 BALANCE 字段并按 ID 排序? @Giorgos Betsos的SQL:

ID   |   BALANCE   |   ACTION_QTY   |   ACTION_TYPE |   PRODUCT_ID   |
---------------------------------------------------------------------|
1    |     0       |       3        |       ADD     |       12       |
2    |     0       |       2        |       DEDUCT  |       12       |  
3    |     0       |       6        |       ADD     |       11       |      
4    |     0       |       3        |       ADD     |       11       | 
5    |     0       |       1        |       DEDUCT  |       12       | 
6    |     0       |       7        |       ADD     |       15       | 
7    |     0       |       1        |       DEDUCT  |       15       | 
8    |     0       |       3        |       ADD     |       15       | 
9    |     0       |       7        |       DEDUCT  |       11       |  
10   |     0       |       9        |       ADD     |       12       |

我尝试添加GROUP BY PRODUCT_ID,但结果不正确。

我想要的结果如下:

SELECT ID, 
       @s := IF(ACTION_TYPE='ADD', @s + ACTION_QTY, @s - ACTION_QTY) AS BALANCE,
       ACTION_QTY,
       ACTION_TYPE
FROM tableA
CROSS JOIN (SELECT @s := 0) AS var
ORDER BY ID 

请帮助我, 非常感谢

2 个答案:

答案 0 :(得分:1)

您可以在相关查询中使用CASE EXPRESSION执行此操作:

SELECT s.id,
       (SELECT sum(CASE WHEN t.action_type = 'ADD' THEN t.action_qty ELSE -1*t.action_qty END)
        FROM (YourQuery) t
        WHERE t.id <= s.id and t.product_id = s.product_id),
       s.action_qty,s.action_type,s.product_id
FROM (YourQuery) s
ORDER BY s.id

该案例将确定是否对该值求和,或将value*-1action_type列中的值相加。

答案 1 :(得分:0)

抱歉,我不确定我的理解是否正确。所以你只想在字段ACTION_TYPE上总结ACTION_QTY?试试这样:

SELECT ID,
       SUM (
         IF(ACTION_TYPE='ADD', ACTION_QTY,  -1 * ACTION_QTY)
       ) AS BALANCE,
       ACTION_QTY,
       ACTION_TYPE
FROM tableA
GROUP BY PRODUCT_ID
ORDER BY PRODUCT_ID;

**样品

创建表格

MariaDB [bb]> CREATE TABLE tableA
    -> (`ID` int, `BALANCE` decimal(10,4), `ACTION_QTY` decimal(10,4), `ACTION_TYPE` varchar(100), `PRODUCT_ID` int(20))
    -> ;
Query OK, 0 rows affected (0.41 sec)

MariaDB [bb]>
MariaDB [bb]> INSERT INTO tableA
    -> (`ID`, `BALANCE`, `ACTION_QTY`, `ACTION_TYPE`, `PRODUCT_ID`)
    -> VALUES
    -> (1, '0.00', '3', 'ADD', '12'),
    ->     (2, '0.00', '2', 'DEDUCT', '12'),
    ->     (3, '0.00', '6', 'ADD', '11'),
    ->     (4, '0.00', '3', 'ADD', '11'),
    ->     (5, '0.00', '1', 'DEDUCT', '12'),
    ->     (6, '0.00', '7', 'ADD', '15'),
    ->     (7, '0.00', '8', 'DEDUCT', '15'),
    ->     (8, '0.00', '3', 'ADD', '15'),
    ->     (9, '0.00', '7', 'DEDUCT', '11'),
    ->     (10,'0.00', '9', 'ADD', '12')
    -> ;
Query OK, 10 rows affected (0.08 sec)
Records: 10  Duplicates: 0  Warnings: 0

运行查询

MariaDB [bb]> SELECT ID,
    ->        SUM(
    ->          IF(ACTION_TYPE='ADD', ACTION_QTY,  -1 * ACTION_QTY)
    ->        ) AS BALANCE,
    ->        ACTION_QTY,
    ->        ACTION_TYPE
    -> FROM tableA
    -> GROUP BY PRODUCT_ID
    -> ORDER BY PRODUCT_ID;
+------+---------+------------+-------------+
| ID   | BALANCE | ACTION_QTY | ACTION_TYPE |
+------+---------+------------+-------------+
|    3 |  2.0000 |     6.0000 | ADD         |
|    1 |  9.0000 |     3.0000 | ADD         |
|    6 |  2.0000 |     7.0000 | ADD         |
+------+---------+------------+-------------+
3 rows in set (0.03 sec)

MariaDB [bb]>