簡體   English   中英

2列上的array_agg()函數

[英]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}            |
+---------+------------+-------------------------+

您將在第二列中混合texttext[]以及在第三列中混合datedate[] 那永遠都行不通。 如果您可以在所有情況下使用數組,那么這應該可以工作:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM