使用二进制结果按列名拼接

huangapple go评论77阅读模式
英文:

Concat by Column Names using Binary Results

问题

Desired Output Table:

ID Flags
1 Flag1, Flag3
2 Flag2, Flag3
英文:

I'd like to concat column names if true as per below:

Input Table:

ID Flag1 Flag2 Flag3
1 True False True
2 False True True

Desired Output Table:

ID Flags
1 Flag1, Flag3
2 Flag2, Flag3

答案1

得分: 1

以下查询使用了CASE表达式和CONCAT函数,在Snowflake中应该适用。

SELECT 
    ID, 
    RTRIM(CONCAT(
        CASE WHEN Flag1 THEN 'Flag1, ' ELSE '' END,
        CASE WHEN Flag2 THEN 'Flag2, ' ELSE '' END,
        CASE WHEN Flag3 THEN 'Flag3, ' ELSE '' END
    ), ', ') AS Flags
FROM 
    YourTable;

输出:

ID Flags
1 Flag1, Flag3
2 Flag2, Flag3
英文:

The following query using a CASE expression and the CONCAT function shall work for you in snowflake.

SELECT 
    ID, 
    RTRIM(CONCAT(
        CASE WHEN Flag1 THEN 'Flag1, ' ELSE '' END,
        CASE WHEN Flag2 THEN 'Flag2, ' ELSE '' END,
        CASE WHEN Flag3 THEN 'Flag3, ' ELSE '' END
    ), ', ') AS Flags
FROM 
    YourTable;

Output :

ID Flags
1 Flag1, Flag3
2 Flag2, Flag3

As I don't have any free tool to show you demo; I am trying to show you demo here using equivalent query in other database

答案2

得分: 1

以下是翻译好的部分:

所以将一些命令串在一起,IFF 用于测试列的值并生成名称或null,ARRAY_CONSTRUCT_COMPACT 用于去掉null值,以及ARRAY_TO_STRING 用于将它们组合得漂亮。

并且使用一个数据的公共表达式(CTE):

with data(id, flag1, flag2, flag3) as (
    select * from values
    (1,	True,	False,	True),
    (2,	False,	True,	True)
)
select 
    id
    ,iff(flag1,'flag1', null) as c1
    ,iff(flag2,'flag2', null) as c2
    ,iff(flag3,'flag3', null) as c3
    ,array_construct_compact(c1,c2,c3) as a
    ,array_to_string(a, ', ') as result
from data
order by 1;
ID C1 C2 C3 A RESULT
1 flag1 null flag3 [ "flag1", "flag3" ] flag1, flag3
2 null flag2 flag3 [ "flag2", "flag3" ] flag2, flag3

这段代码可以折叠成以下形式:

```sql
select 
    id
    ,array_to_string( 
        array_construct_compact(
            iff(flag1,'flag1', null),
            iff(flag2,'flag2', null), 
            iff(flag3,'flag3', null)
            )
        , ', '
        ) as flags
from data
order by 1;

得到结果:

ID FLAGS
1 flag1, flag3
2 flag2, flag3
英文:

so chaining a few commands together, IFF to test the column value and produce the name or null, ARRAY_CONSTRUCT_COMPACT to strip out the null values, and ARRAY_TO_STRING to put it together nicely.

and using a CTE for data:

with data(id, flag1, flag2, flag3) as (
    select * from values
    (1,	True,	False,	True),
    (2,	False,	True,	True)
)
select 
    id
    ,iff(flag1,'flag1', null) as c1
    ,iff(flag2,'flag2', null) as c2
    ,iff(flag3,'flag3', null) as c3
    ,array_construct_compact(c1,c2,c3) as a
    ,array_to_string(a, ', ') as result
from data
order by 1;
ID C1 C2 C3 A RESULT
1 flag1 null flag3 [ "flag1", "flag3" ] flag1, flag3
2 null flag2 flag3 [ "flag2", "flag3" ] flag2, flag3

which can be folded up like:

select 
    id
    ,array_to_string( 
        array_construct_compact(
            iff(flag1,'flag1', null),
            iff(flag2,'flag2', null), 
            iff(flag3,'flag3', null)
            )
        , ', '
        ) as flags
from data
order by 1;

to give:

ID FLAGS
1 flag1, flag3
2 flag2, flag3

答案3

得分: 0

您可以使用CASE语句、CONCAT函数和字符串聚合函数,就像您可以使用LISTAGG函数一样。

这是一个示例SQL查询:

SELECT ID, LISTAGG(CASE
                    WHEN Flag1 = TRUE THEN 'Flag1'
                    ELSE NULL
                  END || ', ' ||
                  CASE
                    WHEN Flag2 = TRUE THEN 'Flag2'
                    ELSE NULL
                  END || ', ' ||
                  CASE
                    WHEN Flag3 = TRUE THEN 'Flag3'
                    ELSE NULL
                  END, ', ') WITHIN GROUP (ORDER BY ID) AS Flags
FROM InputTable
GROUP BY ID;
英文:

You can use a combination of the CASE statement, CONCAT function, and string aggregation function like you can use the LISTAGG function.

Here's an example SQL query:

SELECT ID, LISTAGG(CASE
                WHEN Flag1 = TRUE THEN 'Flag1'
                ELSE NULL
              END || ', ' ||
              CASE
                WHEN Flag2 = TRUE THEN 'Flag2'
                ELSE NULL
              END || ', ' ||
              CASE
                WHEN Flag3 = TRUE THEN 'Flag3'
                ELSE NULL
              END, ', ') WITHIN GROUP (ORDER BY ID) AS Flags
FROM InputTable
GROUP BY ID;

huangapple
  • 本文由 发表于 2023年4月7日 02:50:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定