Azima
Azima

Reputation: 4151

group multiple rows with the same value mysql

I have a certain result-set and I need to group them together on userId.

e.g.

userId  2019-01-15  2019-01-16
------------------------------
132     0           30_140
132     30_140      0

Required output:

userId  2019-01-15  2019-01-16
------------------------------
132     30_140      30_140

Since values contain non-numeric characters, SUM won't work.

Upvotes: 1

Views: 374

Answers (1)

Nick
Nick

Reputation: 147256

If the empty values are all 0 or NULL you can just use MAX:

SELECT userID, MAX(`2019-01-15`) AS `2019-01-15`, MAX(`2019-01-16`) AS `2019-01-16`
FROM test
GROUP BY userID

Output:

userID  2019-01-15  2019-01-16
132     30_140      30_140

Demo on dbfiddle

Upvotes: 4

Related Questions