0

I have database value, for example

table name: a_table

===================================
| userId       | userName         |
===================================
===================================
| abc          | Alice            |
| bcd          | Rachel           |
| efg          | Raymond          |
===================================

table name: b_transaction

=============================================
| transCode     | userId       | value      |
=============================================
=============================================
| 1             | abc          | 100        |
| 2             | abc          | -200       |
| 3             | abc          | 300        |
=============================================

My goal is, get sum of all data, if user dont have row in table transaction, they must be 0. But when I try this query

SELECT a.userId, a.userName, SUM(b.value)
FROM a_table a
LEFT JOIN b_transaction b ON a.userId = b.userId

The result just return 1 row

================================================
| userId       | userName         | value      |
================================================
================================================
| abc          | Alice            | 200        |
================================================

How to achieve that? Thaanks~

1
  • you need to add at the end of query, GROUP BY a.userId Commented Jan 14, 2020 at 9:46

4 Answers 4

2

You could try grouping the results on the users over the summing of the transactions, like this?

SELECT a.userId, a.userName, SUM(b.value)
FROM a_table a
LEFT JOIN b_transaction b ON a.userId = b.userId
GROUP BY a.userId, a.userName
Sign up to request clarification or add additional context in comments.

Comments

1

add group by:

SELECT a.userId, a.userName, IFNULL (SUM(b.value),0)
FROM a_table a
LEFT JOIN b_transaction b ON a.userId = b.userId
GROUP BY  a.userId, a.userName

see the sqlfiddle

Comments

1

Using a simple group by statement in your SQL should create the desired result for example:

SELECT a.userId, a.userName, SUM(b.value)
FROM a_table a
LEFT JOIN b_transaction b ON a.userId = b.userId GROUP BY a.userId

1 Comment

Always groub all columns that are part of the SELECT clause. Else you get an error in most databases
1

You can use IFNULL to check if the result of the sum is null (that is the case if there is no reference for a row from a to b) or not, and then apply a default value :

SELECT a.userId, a.userName, IFNULL(SUM(b.value), 0) AS 'Sum'
FROM a_table a
LEFT JOIN b_transaction b ON a.userId = b.userId
GROUP BY a.userId, a.userName

This outputs :

| userId | userName | Sum |
| ------ | -------- | --- |
| abc    | Alice    | 200 |
| bcd    | Rachel   | 0   |
| efg    | Raymond  | 0   |

2 Comments

I think this is not correct IFNULL(SUM(b.value), 0) it should be SUM (IF(b.value is not null, b.value, 0)) because you are overriding 'Sum' value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.