Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 雪花:基于条件的ListAgg数据_Sql_String_Subquery_Snowflake Cloud Data Platform_Window Functions - Fatal编程技术网

Sql 雪花:基于条件的ListAgg数据

Sql 雪花:基于条件的ListAgg数据,sql,string,subquery,snowflake-cloud-data-platform,window-functions,Sql,String,Subquery,Snowflake Cloud Data Platform,Window Functions,我有一个表p,看起来像这样: ID | Status | Env 1 | 1 | Linux 1 | 1 | Windows 1 | 3 | Mac 2 | 1 | Linux 2 | 1 | Windows 2 | 1 | Mac 3 | 3 | Linux 3 | 0 | Windows 3 | 3 | Mac 这里,1表示测试成功,而任何其他数字表示某种失败。我希望以这样的方式聚合

我有一个表
p
,看起来像这样:

ID | Status | Env
1  |   1    | Linux
1  |   1    | Windows
1  |   3    | Mac
2  |   1    | Linux
2  |   1    | Windows
2  |   1    | Mac
3  |   3    | Linux
3  |   0    | Windows
3  |   3    | Mac
这里,
1
表示测试成功,而任何其他数字表示某种失败。我希望以这样的方式聚合这些数据:对于每个失败的测试,我在每一行中都有一个以逗号分隔的失败环境列表。如果没有失败,则新列中应该有
NULL
。输出将类似于

ID | Status | Env     | Failure_list
1  |   1    | Linux   | Mac
1  |   1    | Windows | Mac
1  |   3    | Mac     | Mac
2  |   1    | Linux   | Null
2  |   1    | Windows | Null
2  |   1    | Mac     | Null
3  |   3    | Linux   | Linux, Windows, Mac
3  |   0    | Windows | Linux, Windows, Mac
3  |   3    | Mac     | Linux, Windows, Mac
我在如下查询中使用snowflake Listag()函数

SELECT ID, STATUS, LISTAGG(ENV, ', ') 
FROM P
GROUP BY ID, STATUS
这是我得到的输出:

ID | Status | Env
1  |   1    | Linux, Windows
1  |   3    | Mac
2  |   1    | Linux, Windows, Mac
3  |   0    | Windows
3  |   3    | Linux, Mac

如何更改此查询以获取我正在查找的输出?

您可以使用相关子查询解决此问题:

select
    t.*,
    (
        select listagg(t1.env)
        from mytable t1
        where t1.id = t.id and t1.status <> 1
    ) failure_list
from mytable t

第二个查询非常适合我所寻找的内容。请告诉我如何在snowflake中更正以下查询。。
select 
    t.*,
    listagg(case when status <> 1 then env end) over(partition by id) failure_list
from mytable t