COUNT(*)和COUNT SQL中的项目列表

时间:2019-02-01 04:58:46

标签: sql oracle

我有一张这样的桌子

Acct_number    account_id 

xxxx            1111

xxxx            2222

xxxx            3333



SELECT DISTINCT acct_number
      ,count(account_id) 
FROM account_table 
GROUP BY acct_number;

如何使我的输出像这样:

acct_number    count_acct_number    account_id

xxxx                 3             1111, 2222 ,33333

1 个答案:

答案 0 :(得分:4)

您可以尝试以下操作-使用listagg()功能,它适用于 11g及更高版本

SELECT 
    acct_number,count(account_id)
    LISTAGG(account_id, ', ') WITHIN GROUP (ORDER BY account_id) "account_id"
FROM account_table
GROUP BY acct_number

或者您可以使用wm_concat()函数

SELECT acct_number,count(account_id)
       wm_concat(account_id) "account_id"
    FROM account_table
    GROUP BY acct_number