[英]Add all distinct values of a column to an array
我正在嘗試將 output 列的不同值放入數組中,而不是SELECT DISTINCT column FROM table
,這將 output 1 行每個唯一值。 我想要只有 1 行。 我已經嘗試了一些方法來做到這一點,但我一直以錯誤告終:
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
我也嘗試了以下產生相同錯誤的方法。
SELECT
ARRAY(SELECT DISTINCT column_1 FROM table WHERE p = '2022-08-17') as column_distinct_values
'Where `p` is the partition
所以問題是,實現這一目標的正確語法/方法是什么?
樣品輸入/所需 output
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
或者
-- query
select array_agg(distinct val)
from dataset
group by true; -- fake grouping
Output:
_col0 |
---|
[諾拉姆,諾拉姆1] |
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.