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

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

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中应该适用。

  1. SELECT
  2. ID,
  3. RTRIM(CONCAT(
  4. CASE WHEN Flag1 THEN 'Flag1, ' ELSE '' END,
  5. CASE WHEN Flag2 THEN 'Flag2, ' ELSE '' END,
  6. CASE WHEN Flag3 THEN 'Flag3, ' ELSE '' END
  7. ), ', ') AS Flags
  8. FROM
  9. 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.

  1. SELECT
  2. ID,
  3. RTRIM(CONCAT(
  4. CASE WHEN Flag1 THEN 'Flag1, ' ELSE '' END,
  5. CASE WHEN Flag2 THEN 'Flag2, ' ELSE '' END,
  6. CASE WHEN Flag3 THEN 'Flag3, ' ELSE '' END
  7. ), ', ') AS Flags
  8. FROM
  9. 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):

  1. with data(id, flag1, flag2, flag3) as (
  2. select * from values
  3. (1, True, False, True),
  4. (2, False, True, True)
  5. )
  6. select
  7. id
  8. ,iff(flag1,'flag1', null) as c1
  9. ,iff(flag2,'flag2', null) as c2
  10. ,iff(flag3,'flag3', null) as c3
  11. ,array_construct_compact(c1,c2,c3) as a
  12. ,array_to_string(a, ', ') as result
  13. from data
  14. 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
  1. 这段代码可以折叠成以下形式:
  2. ```sql
  3. select
  4. id
  5. ,array_to_string(
  6. array_construct_compact(
  7. iff(flag1,'flag1', null),
  8. iff(flag2,'flag2', null),
  9. iff(flag3,'flag3', null)
  10. )
  11. , ', '
  12. ) as flags
  13. from data
  14. 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:

  1. with data(id, flag1, flag2, flag3) as (
  2. select * from values
  3. (1, True, False, True),
  4. (2, False, True, True)
  5. )
  6. select
  7. id
  8. ,iff(flag1,'flag1', null) as c1
  9. ,iff(flag2,'flag2', null) as c2
  10. ,iff(flag3,'flag3', null) as c3
  11. ,array_construct_compact(c1,c2,c3) as a
  12. ,array_to_string(a, ', ') as result
  13. from data
  14. 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:

  1. select
  2. id
  3. ,array_to_string(
  4. array_construct_compact(
  5. iff(flag1,'flag1', null),
  6. iff(flag2,'flag2', null),
  7. iff(flag3,'flag3', null)
  8. )
  9. , ', '
  10. ) as flags
  11. from data
  12. order by 1;

to give:

ID FLAGS
1 flag1, flag3
2 flag2, flag3

答案3

得分: 0

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

这是一个示例SQL查询:

  1. SELECT ID, LISTAGG(CASE
  2. WHEN Flag1 = TRUE THEN 'Flag1'
  3. ELSE NULL
  4. END || ', ' ||
  5. CASE
  6. WHEN Flag2 = TRUE THEN 'Flag2'
  7. ELSE NULL
  8. END || ', ' ||
  9. CASE
  10. WHEN Flag3 = TRUE THEN 'Flag3'
  11. ELSE NULL
  12. END, ', ') WITHIN GROUP (ORDER BY ID) AS Flags
  13. FROM InputTable
  14. 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:

  1. SELECT ID, LISTAGG(CASE
  2. WHEN Flag1 = TRUE THEN 'Flag1'
  3. ELSE NULL
  4. END || ', ' ||
  5. CASE
  6. WHEN Flag2 = TRUE THEN 'Flag2'
  7. ELSE NULL
  8. END || ', ' ||
  9. CASE
  10. WHEN Flag3 = TRUE THEN 'Flag3'
  11. ELSE NULL
  12. END, ', ') WITHIN GROUP (ORDER BY ID) AS Flags
  13. FROM InputTable
  14. 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:

确定