I am doing a migration from MSSQL to PostgreSQL
MSSQL query:
SELECT * FROM dbo.Mtr_userdetails AS table1 JOIN( SELECT urmo.urm_userid STUFF((SELECT ',' + urm.urm_role_name FROM dbo.STL_CS_Mtr_userrolemapping urm WHERE urm.urm_userid = 'test2' AND urm.urm_status = 'A' AND urm.urm_appid = 'BCA' FOR XML PATH('')),1,1,'') [user_roles],urmo.urm_appid FROM dbo.Mtr_userrolemapping urmo WHERE urmo.urm_appid = 'BCA' AND urmo.urm_userid = 'test2' GROUP BY urmo.urm_userid,urmo.urm_appid) AS table2 ON table1.ud_userid = table2.urm_userid WHERE (table1.ud_userid = 'test2')
I am trying to convert the above ms sql query to postgresql syntax.
PostgreSQL query:
SELECT *
FROM STL_Mtr_userdetails AS table1
JOIN (
SELECT urmo.urm_userid,
string_agg((SELECT ',' || urm.urm_role_name
FROM STL_CS_Mtr_userrolemapping urm
WHERE urm.urm_userid = 'test2'
AND urm.urm_status = 'A'
AND urm.urm_appid = 'BCA')::varchar, 1::varchar, 1::varchar, ''::varchar) user_roles,
urmo.urm_appid
FROM STL_CS_Mtr_userrolemapping urmo
WHERE urmo.urm_appid = 'BCA'
AND urmo.urm_userid = 'test2'
GROUP BY urmo.urm_userid,
urmo.urm_appid
) AS table2 ON table1.ud_userid = table2.urm_userid
WHERE (table1.ud_userid = 'test2')
I am facing the below error when executing my postgresql query
question from:https://stackoverflow.com/questions/66065401/string-aggcharacter-varying-doesnt-existstring_agg(character varying,character varying,character varying,character varying) does not exist. No hint matches the given name and argument types. you might need to add explicit type casts.