简体   繁体   中英

How to concatenate all entries returned by a joined subquery (related to the main table id) into a single combined column?

Let's say I have a main driver table called Employee with the following data and columns:

 EMPLOYEE_ID    EMPLOYEE_NAME      EMPLOYEE_CAR     EMPLOYEE_AGE
 ----------------------------------------------------------------
     1          Mike               Camry                35          
     2          Mark               Civic                33
     3          Helen              Beetle               25

And I have another table called Employee_Feedback with the following data and columns:

 EMPLOYEE_FEEDBACK_ID     EMPLOYEE_ID       FEEDBACK_COMMENT_TX
 ---------------------------------------------------------------
        1                      1              Very Good
        2                      1              Average
        3                      1              Phenomenal
        4                      2              Okay
        5                      2              NO Comment
        6                      3              Excellent
        7                      3              Hilarious

With the data from the Employee and Employee_Feedback tables, I want my query to be able to return all of the rows in Employee and also to concatenate all of the 'related' employee feedback_comments from Employee_feedback into a single column per employee. It would look like this:

 EMPLOYEE_NAME    EMPLOYEE_CAR     EMPLOYEE_AGE   Comments
 ------------------------------------------------------------------------
 Mike             Camry              35           Very Good Average Phenomenal
 Mark             Civic              33           Okay No Comment 
 Helen            Beetle             25           Excellent Hilarious

What would be a good query to do this? I've tried the following with no success:

select employee_name
     , employee_car
     , employee_age
     , listagg(feedback_comment_Tx, ' ') within group (order by e.employee_id)
  from employee e
  join employee_feedback ef ON ef.employee_id = e.employee_id;

Am I missing a minor detail? Thanks in advance!

You need to add a group by clause at the end :

group by employee_name, employee_car, employee_age

Edit : including your filters in the comment, you may try the following :

select employee_name
     , employee_car
     , employee_age
     , listagg(feedback_comment_Tx, ' ') within group (order by e.employee_id)
  from employee e
  join employee_feedback ef ON ef.employee_id = e.employee_id
 where nvl(initcap(feedback_comment_Tx),'No Comment') != 'No Comment'
 group by employee_name, employee_car, employee_age;

Have you tried to add group by e.employee_id at the end of query like that

select employee_name
 , employee_car
 , employee_age
 , listagg(feedback_comment_Tx, ' ') within group (order by e.employee_id) 
   from employee e join employee_feedback ef ON ef.employee_id = e.employee_id 
    group by e.employee_id
select employee_name
     , employee_car
     , employee_age
     , listagg(feedback_comment_Tx, ' ') within group (order by ef.EMPLOYEE_FEEDBACK_ID)
  from employee e
  join employee_feedback ef ON ef.employee_id = e.employee_id
  group by e.employee_id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM