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;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744360186a4570431.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