[英]array_agg() function on 2 columns
我在下面的桌子
CUST_ID integer | servername text | DATE
--------------------- |---------------------------------------
1 | '1' | '2017-01-15'
1 | '1' | '2017-02-15'
2 | '1' | '2017-01-15'
2 | '2' | '2017-01-15'
我想這樣提取信息。
CUST_ID integer | servername text | DATE
-------------------------|------------------------|------------------------------
1 | '1' | {'2017-01-15', '2017-02-15'}
2 | {'1', '2'} | '2017-01-15'
我已經嘗試了GROUP BY CUST_ID, DATE
UNION GROUP BY CUST_ID, SERVICE
這樣。 顯然我收到一個ERROR: UNION types text and text[] cannot be matched
因為在第一個選擇查詢中, servername
不是數組。
SELECT CUST_ID, servername, array_agg(trans_date) FROM infomation
GROUP by CUST_ID, servername
UNION
SELECT CUST_ID, array_agg(servername), trans_date FROM infomation
GROUP by CUST_ID, trans_date;
select cust_id
,array_agg(distinct servername) as servername
,array_agg(distinct date) as date
from information
group by cust_id
+---------+------------+-------------------------+
| cust_id | servername | date |
+---------+------------+-------------------------+
| 1 | {"1"} | {2017-01-15,2017-02-15} |
| 2 | {"1","2"} | {2017-01-15} |
+---------+------------+-------------------------+
您將在第二列中混合text
和text[]
以及在第三列中混合date
和date[]
。 那永遠都行不通。 如果您可以在所有情況下使用數組,那么這應該可以工作:
SELECT cust_id,
array_agg(DISTINCT servername) AS service,
array_agg(DISTINCT trans_date) AS trans_date
FROM information
GROUP BY cust_id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.