admin管理员组文章数量:1402802
How can I aggregate some tuples like this
COL_1 | COL_2 | COL_3
val | Abc | Null
val | Null | CDe
with the OR function and return the following table?
COL_1 | COL_2 | COL_3
val | Abc | CDe
How can I aggregate some tuples like this
COL_1 | COL_2 | COL_3
val | Abc | Null
val | Null | CDe
with the OR function and return the following table?
COL_1 | COL_2 | COL_3
val | Abc | CDe
Share
Improve this question
edited Mar 21 at 10:27
Panagiotis Kanavos
132k16 gold badges203 silver badges265 bronze badges
asked Mar 21 at 10:26
Riya BansalRiya Bansal
1,3111 gold badge11 silver badges10 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default 0I don't think OR is the right way to solve this since you must aggregate the records. Just use an aggregate function like MIN(), MAX(), ARRAY_AGG() or ANY_VALUE(). Even STRING_AGG() could be an option.
SELECT col_1
, MIN(col_2) -- MAX() or ANY_VALUE() could also work
, MIN(col_3)
FROM (VALUES
('val','Abc',Null)
,('val',Null, 'CDe')
) t1(COL_1, COL_2, COL_3 )
GROUP BY col_1
ORDER BY col_1;
本文标签: How can I aggregate like this in postgresqlStack Overflow
版权声明:本文标题:How can I aggregate like this in postgresql - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744360185a2602505.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
OR
doesn't aggregate. What do you mean by aggregation to begin with? What happens if there are more than 2 rows?MIN
orMAX
applied to the columns you posted would return the results you want for these 2 rows. What if there's a 3rd row, whereCOL_3
containsEfg
? Do you wantCDe
orEfg
in the result? – Panagiotis Kanavos Commented Mar 21 at 10:28MIN
orMAX
would produce the results you want, egSELECT MIN(COL_1), MIN(COL_2), MIN(COL_3) FROM ThatTable
. Functions likeMIN
andMAX
eliminate NULLs – Panagiotis Kanavos Commented Mar 21 at 10:34