总结类似的正负值行,以在MySQL

时间:2018-05-03 12:23:40

标签: mysql

我有一个transaction表,我需要在其中生成摘要。

+---------------+------------+------------+--------+--------+
|transaction_id | student_id | payment_id | amount | action |
+---------------+------------+------------+--------+--------+
|             1 |          1 |           1|    100 |      1 |
|             2 |          1 |           2|     50 |      1 |
|             3 |          1 |           3|      0 |      1 |
|             4 |          1 |           4|     25 |      1 |
|             5 |          1 |           5|    200 |      1 |
|             6 |          2 |           1|    100 |      1 |
|             7 |          2 |           2|     50 |      1 |
|             8 |          2 |           2|    -50 |      0 |
|             9 |          2 |           4|    -25 |      0 |
|            10 |          2 |           5|   -200 |      0 |
|            11 |          3 |           2|     75 |      1 |
|            12 |          3 |           4|    -25 |      0 |
+---------------+------------+------------+--------+--------+

(行动:1 =付款,0 =取消)

我可以通过以下查询获得交易摘要。

SELECT payment_id, amount, COUNT(amount) AS student_count, SUM(amount) AS total_amount
  FROM transaction t
    GROUP BY payment_id, amount;

结果是。

+------------+--------+---------------+--------------+
| payment_id | amount | student_count | total_amount |
+------------+--------+---------------+--------------+
|          1 |    100 |             2 |          200 |
|          2 |    -50 |             1 |          -50 |
|          2 |     50 |             2 |          100 |
|          2 |     75 |             1 |           75 |
|          3 |      0 |             1 |            0 |
|          4 |    -25 |             2 |          -50 |
|          4 |     25 |             1 |           25 |
|          5 |    200 |             1 |          200 |
|          5 |   -200 |             1 |         -200 |
+------------+--------+---------------+--------------+

但是,总而言之,我想总结每个相似数量的加号和减号值,以保持清洁。因此,需求是一个输出,如下面的总和加号和减号值来显示结果。 如果它们总和为零,那么该行不需要显示。 < /强>

+------------+--------+---------------+--------------+
| payment_id | amount | student_count | total_amount |
+------------+--------+---------------+--------------+
|          1 |    100 |             2 |          200 |
|          2 |     50 |             1 |           50 |
|          2 |     75 |             1 |           75 |
|          3 |      0 |             1 |            0 |
|          4 |    -25 |             1 |          -25 |
+------------+--------+---------------+--------------+

如果有人可以帮助在MySQL中实现这一点,我们非常感激。

2 个答案:

答案 0 :(得分:1)

根据您提供的(更新的)数据,我认为此查询(SQLFiddle)应该产生您想要的内容:

SELECT 
  payment_id,
  ABS(amount) AS amount,
  ABS(SUM(IF(action=1,1,-1))) AS student_count,
  SUM(amount) AS total_amount
FROM transaction
GROUP BY payment_id, ABS(amount)
HAVING total_amount != 0 OR amount = 0

输出:

payment_id  amount  student_count   total_amount
1           100     2               200
2           50      1               50
2           75      1               75
3           0       1               0
4           25      1               -25

如果您希望payment_id 4的金额为负数(以匹配total_amount),请将查询的第三行更改为(已更新SQLFiddle

  ABS(amount)*IF(SUM(amount)>0,1,-1) AS amount,

那会给出

4           -25     1               -25

答案 1 :(得分:0)

您应该仅在payment_id上汇总数据。

SELECT 
  payment_id,
  MAX(amount) amount,
  (COUNT(DISTINCT student_id) - SUM(amount<0)) student_count,
  SUM(amount) total_amount
FROM `transaction`
GROUP BY payment_id;  

请参阅DEMO on SQL Fiddle