我如何总结子表?

时间:2014-11-27 07:32:29

标签: mysql sql select group-by sum

我已经尝试过此查询,但结果错误了...... 我查看了许多参考文献,但我找不到答案。

SELECT a.id, SUM(b1.amount) as section1, SUM(ABS(b2.amount)) as section2 FROM parent_table as a
INNER JOIN child_table as b1 ON a.id=b1.parent_id
INNER JOIN child_table as b2 ON a.id=b2.parent_id
WHERE b1.bh_status IN ('section1') AND b2.bh_status IN ('section2')
GROUP BY a.id

表格是:

parent_table

id  | customer_namem | date_register
----+----------------+--------------
1   | customer1      | 2014-20-11   
2   | customer2      | 2014-21-11   
3   | customer3      | 2014-22-11   
4   | customer4      | 2014-23-11   
5   | customer5      | 2014-23-11   

child_table

id  | ch_key    | ch_value | parent_id 
----+-----------+----------+----------
1   | deposit   | 100      | 1
2   | deposit   | 500      | 1
3   | withdraw  | 100      | 1
4   | withdraw  | 100      | 1
5   | deposit   | 10       | 2
6   | deposit   | 100      | 2
7   | deposit   | 50       | 3
8   | deposit   | 50       | 3
9   | withdraw  | 10       | 3
10  | deposit   | 50       | 4
11  | withdraw  | 50       | 4
12  | withdraw  | 150      | 5

我想这样显示:

id  | customer_namem | deposit | withdtaw | date_register
1   | customer1      | 600     | 200      | 2014-20-11  
2   | customer2      | 110     | 0        | 2014-20-11  
3   | customer3      | 100     | 10       | 2014-20-11  
4   | customer4      | 50      | 50       | 2014-20-11  
5   | customer5      | 0       | 150      | 2014-20-11  

3 个答案:

答案 0 :(得分:0)

您必须使用CASE,如下所示:

SELECT 
  a.id, a.customer_namem, 
  SUM(CASE WHEN ch_key = "deposit" THEN ch_value ELSE 0 END) as deposit,
  SUM(CASE WHEN ch_key = "withdraw" THEN ch_value ELSE 0 END) as withdraw,
  a.date_register
FROM parent_table as a
INNER JOIN child_table as b ON a.id=b.parent_id
GROUP BY a.id

工作演示:http://sqlfiddle.com/#!2/01ad95/1

答案 1 :(得分:0)

试试这个:

SELECT a.id, a.customer_namem,
         SUM(CASE WHEN b.ch_key = 'deposit' THEN b.amount ELSE 0 END) AS deposit, 
         SUM(CASE WHEN b.ch_key = 'withdraw' THEN b.amount ELSE 0 END) AS withdraw, 
         a.date_register 
FROM parent_table AS a
LEFT OUTER JOIN child_table AS b ON a.id = b.parent_id
GROUP BY a.id;

SELECT a.id, a.customer_namem,
         SUM(IF(b.ch_key = 'deposit', b.amount, 0)) AS deposit, 
         SUM(IF(b.ch_key = 'withdraw', b.amount, 0)) AS withdraw, 
         a.date_register 
FROM parent_table AS a
LEFT OUTER JOIN child_table AS b ON a.id = b.parent_id
GROUP BY a.id;

答案 2 :(得分:0)

查询

SELECT a.id,
a.customer_name,
COALESCE(SUM(CASE WHEN b.ch_key='deposit' THEN b.ch_value END),0) AS deposit,
COALESCE(SUM(CASE WHEN b.ch_key='withdraw' THEN b.ch_value END),0) AS withdraw,
a.date_register
FROM parent_table a
JOIN child_table b
ON a.id=b.parent_id
GROUP BY a.id;

<强>输出

+----+---------------+---------+----------+---------------+
| ID | CUSTOMER_NAME | DEPOSIT | WITHDRAW | DATE_REGISTER |
+----+---------------+---------+----------+---------------+
|  1 |    customer 1 |     600 |      200 | 2014-11-20    |
|  2 |    customer 2 |     110 |        0 | 2014-11-21    |
|  3 |    customer 3 |     100 |       10 | 2014-11-22    |
|  4 |    customer 4 |      50 |       50 | 2014-11-23    |
|  5 |    customer 5 |       0 |      150 | 2014-11-23    |
+----+---------------+---------+----------+---------------+

Fiddle demo