我有一个这样的表:
+--+---------+---------+
|id|str_value|int_value|
+--+---------+---------+
| 1| 'abc' | |
| 1| | 1 |
| 2| 'abcd' | |
| 2| | 2 |
+--+---------+---------+
我需要得到这个:
+--+---------+---------+
|id|str_value|int_value|
+--+---------+---------+
| 1| 'abc' | 1 |
| 2| 'abcd' | 2 |
+--+---------+---------+
在我看来,我需要这样的东西:
select id, first_not_null(str_value), first_not_null(int_value)
from table
group by id
有什么可以接受的方法吗?我使用Postgresql 9.0.1.
更新:这应该也适用于uid类型
您应该查看http://www.postgresql.org/docs/8.1/static/functions-aggregate.html查看聚合函数。
我想应该由max来做这项工作
编辑:工作示例
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