Search code examples
sqloracle-databasepivotlistagg

get one value from multiple values in Oracle SQL


I tried the below query which returns two values but i need to get one value separated with ','. For example the query should return result :

TRPT,PRLD

Below is the query i tried:

SELECT     
    LISTAGG(T_NAME, ',') WITHIN GROUP (ORDER BY T_NAME) "ListValues"
FROM TST_TBL where T_DATE > sysdate-1 
GROUP BY T_NAME

It returns:

ListValues
    TRPT
    PRLD

Solution

  • Remove the GROUP BY clause, it causes your "problems".

    SQL> with tst_tbl (t_name, t_date) as
      2    (select 'TRPT', sysdate from dual union all
      3     select 'PRLD', sysdate from dual
      4    )
      5  SELECT
      6      LISTAGG(T_NAME, ',') WITHIN GROUP (ORDER BY T_NAME) "ListValues"
      7  FROM TST_TBL where T_DATE > sysdate-1
      8  /
    
    ListValues
    --------------------
    PRLD,TRPT
    
    SQL>