I have a below query where I need to do a DISTINCT ON
the allowed_id column from the union result, as is possible in PostgreSQL. I have read that Snowflake uses similar kind of PostgreSQL but DISTINCT ON
didn't work.
select distinct on (allowed_id), * from (
select listagg(distinct id) as allowed_id, count (people) as totalpeople ,max(score) as maxscore , min(score) as minscore, 'n' as type from tableA
where userid = 123
union
select listagg(distinct id) as allowed_id, count (people) as totalpeople, max(elscore) as maxscore , min(elscore) as minscore, 'o' as type from tableB
where userid = 123
union
select listagg(distinct id) as allowed_id, null, null , null , 'j' as type from tableC
where userid = 123
union
select listagg(distinct id) as allowed_id, null, null , null , 'a' as type from tableD
where userid = 123
)