0

How to concatenate rcm_working_paper and documents code is below-

select  RCM.id as rcm_no,  
       string_agg(distinct RWP.body->>'working_paper_code', ',')as rcm_working_paper,   
       string_agg(distinct RWP.body->>'document', ',')as documents
from masters."RCM" RCM
  inner join  masters."RCM_WORKING_PAPERS" RWP on RCM.id=RWP.rcm_id
GROUP BY RCM.id

I am doing that way and occurred some error.

select  
    RCM.id as rcm_no,  
    string_agg(distinct RWP.body->>'working_paper_code'|| ' ' || RWP.body->>'document', ',')as 
    rcm_working_paper
from masters."RCM" RCM
  inner join  masters."RCM_WORKING_PAPERS" RWP on RCM.id=RWP.rcm_id
GROUP BY RCM.id 
3
  • And what is the exact error message you get? Commented Jan 21, 2020 at 10:59
  • ERROR: operator does not exist: text ->> unknown LINE 3: ... RWP.body->>'working_paper_code'|| ' ' || RWP.body->>'docume... ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. SQL state: 42883 Character: 115 Commented Jan 21, 2020 at 11:01
  • datatype is jsonb Commented Jan 21, 2020 at 11:05

1 Answer 1

1

You need to enclose each expression in parentheses:

select  
  RCM.id as rcm_no,  
  string_agg(distinct (RWP.body->> 'working_paper_code')|| ' ' ||(RWP.body->>'document'), ',') as rcm_working_paper
from masters."RCM" RCM
  inner join  masters."RCM_WORKING_PAPERS" RWP on RCM.id=RWP.rcm_id
GROUP BY RCM.id 

Alternatively use concat_ws()

select  
   RCM.id as rcm_no,  
   string_agg(distinct concat_ws(' ', RWP.body->> 'working_paper_code', RWP.body->>'document'), ',') as rcm_working_paper
from masters."RCM" RCM
  inner join  masters."RCM_WORKING_PAPERS" RWP on RCM.id=RWP.rcm_id
GROUP BY RCM.id 

Or concat()

select  
   RCM.id as rcm_no,  
   string_agg(distinct concat(RWP.body->> 'working_paper_code', ' ', RWP.body->>'document'), ',') as rcm_working_paper
from masters."RCM" RCM
  inner join  masters."RCM_WORKING_PAPERS" RWP on RCM.id=RWP.rcm_id
GROUP BY RCM.id 
Sign up to request clarification or add additional context in comments.

1 Comment

@RAHULSONI if this answer solved your problem, please mark it as resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.