0

I have a table like this:

+--+---------+---------+
|id|str_value|int_value|
+--+---------+---------+
| 1| 'abc'   |         |
| 1|         |    1    |
| 2| 'abcd'  |         |
| 2|         |    2    |
+--+---------+---------+

I need to get this:

+--+---------+---------+
|id|str_value|int_value|
+--+---------+---------+
| 1| 'abc'   |    1    |
| 2| 'abcd'  |    2    |
+--+---------+---------+

It seems to me that I need something like:

select id, first_not_null(str_value), first_not_null(int_value)
from table
group by id

Is there any acceptable way to do this? I use Postgresql 9.0.1.

Update: this should work with uuid types as well

Alex
  • 1,186
  • 10
  • 12

1 Answers1

0

You should look at http://www.postgresql.org/docs/8.1/static/functions-aggregate.html for aggregate functions.

I guess max should do the work

EDIT: Working example

select id, max(col1), max(col2) from (
    select 1 as id, null as col1, 'test' as col2
    union 
    select 1 as id ,'blah' as col1, null as col2
)  x group by id
zimi
  • 1,586
  • 12
  • 27
  • Thanks! I used that as a temporary solution. But I suspect there will be some more uuid_value columns and your solution wont fit. – Alex Jun 20 '13 at 11:10
  • I used max(uuid_value::varchar(36)) with uuids. This is a little bit slower but works. Thank you. – Alex Jun 21 '13 at 08:43