简体   繁体   English

使用雪花 sql 将多行平展为 1 行

[英]flatten multiple rows in to 1 row using snowflake sql

i have a table like this:我有一张这样的桌子:

id  col1 col2 col3 col4
1   Y    N    N    N  
1   N    N    Y    N
1   N    Y    N    N
1   N    N    N    N

i want it to look like this:我希望它看起来像这样:

id col1 col2 col3 col4
1  Y    Y    Y    N

how can i do this我怎样才能做到这一点

You can use MAX() :您可以使用MAX()

select id, max(col1), max(col2), max(col3), max(col4)
from t
group by id;

If the value to want is not naturally high (and thus you can use MAX) or not naturally low (and thus use MIN) then you can and you are looking for a single state you can encode that into如果想要的值不是自然高(因此您可以使用 MAX)或自然低(因此使用 MIN),那么您可以并且您正在寻找可以将其编码为的单个状态

IFF(SUM(IFF(value = <target>,1,0)>0,<target>,<non-target>)

which really should be done via BOOLOR_AGG (which has the exact example you have posted).这确实应该通过BOOLOR_AGG完成(其中包含您发布的确切示例)。

select id, boolor_agg(col1), boolor_agg(col2), boolor_agg(col3), maxboolor_agg(col4)
from t
group by id;

Otherwise if you really how some more complex value aggregation beyond the assumed logical or it appears you want, you will need to explain more.否则,如果你真的超出了假设的logical or看起来你想要更复杂的值聚合,你将需要解释更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM