leflic leflic - 1 year ago 80
MySQL Question

Summarize a table where some fields are null values

I've got an MySQL table that looks that way:

enter image description here

Now I want to group the three values on the right, so that there are no null values left.
So, in total, in this example I would have two rows, each filled with values.

I tried a lot with group statements, but it never goes the way I want.
Any Ideas?

Answer Source

You can aggregate your table according to records sharing the same t1qrid, start, and ende values. Then use the aggregate function SUM to combine the other three columns. Note that SUM will ignore NULL values, which seems to be the behavior you want.

SELECT t1qrid,
       SUM(Result) AS Result,
       SUM(InclLeft) AS InclLeft,
       SUM(InclRight) AS InclRight
FROM yourTable
GROUP BY t1qrid,
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download