Skip to content
Advertisement

How to overlap NULL values in MYSQL when using group by?

This is my current table, let’s call it “TABLE”

enter image description here

I want end result to be:

enter image description here

I tried this query:

SELECT * FROM TABLE GROUP BY(service)   

but it doesn’t work

i tried replacing NULL with 0 and then perform group by but “TBA” (text value) is creating problem, kindly help me out!

Advertisement

Answer

This looks like simple aggregation:

select service, max(for1) for1, max(for2) for2, max(for3) for3
from mytable
group by service

This takes advantage of the fact that aggregate functions such as max() ignore null values. However if a column has more than one non-null value for a given service, only the greatest will appear in the resultset.

It is unclear what the datatype of your columns is. Different datatypes have different rules for sorting.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement