简体   繁体   English

将列的所有不同值添加到数组

[英]Add all distinct values of a column to an array

I'm trying to output the distinct values of a column into an array instead of SELECT DISTINCT column FROM table which would output 1 row per unique value.我正在尝试将 output 列的不同值放入数组中,而不是SELECT DISTINCT column FROM table ,这将 output 1 行每个唯一值。 I am wanting to have just 1 row.我想要只有 1 行。 I have tried a few method to do this but I keep ending up with the error:我已经尝试了一些方法来做到这一点,但我一直以错误告终:

syntax error: no viable alternative at input 'ARRAY_DISTINCT(SELECT' . syntax error: no viable alternative at input 'ARRAY_DISTINCT(SELECT'

SELECT
    ARRAY_DISTINCT(SELECT column_1 FROM table WHERE p = '2022-08-17') as column_distinct_values

'Where `p` is the partition

I have also tried the below method which yields the same error.我也尝试了以下产生相同错误的方法。

SELECT
    ARRAY(SELECT DISTINCT column_1 FROM table WHERE p = '2022-08-17') as column_distinct_values

'Where `p` is the partition

So the question is, what is the correct syntax/method to achieve this?所以问题是,实现这一目标的正确语法/方法是什么?


Sample input/desired output样品输入/所需 output

在此处输入图像描述

SELECT column_1 FROM table WHERE p = '2022-08-17' does not return an array, if you want to compact everything into one cell you can try group by with aggregation into array: SELECT column_1 FROM table WHERE p = '2022-08-17'不返回数组,如果要将所有内容压缩到一个单元格中,可以尝试group by

-- sample data
WITH dataset (val) AS (
    VALUES ('NORAM'),
    ('NORAM'),
    ('NORAM1')
)

-- query
select array_distinct(array_agg(val))
from dataset
group by true; -- fake grouping

Or或者

-- query
select array_agg(distinct val) 
from dataset 
group by true; -- fake grouping

Output: Output:

_col0 _col0
[NORAM, NORAM1] [诺拉姆,诺拉姆1]

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

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