Hey all I have the following SQL Query that I am using in order to get comma separated values from a cell:
WITH cte AS(
SELECT
uT.id AS UID,
uT.employeeID,
uP.type,
pP.name
FROM
usersTbl AS uT
CROSS APPLY
dbo.DelimitedSplit8K(uT.userPerms,',') AS uPcommaItems
INNER JOIN
usersPermissions uP ON uP.id = uPcommaItems.Item
CROSS APPLY
dbo.DelimitedSplit8K(uT.userPermPages,',') AS pPcommaItems
INNER JOIN
pagePermissions pP ON pP.id = pPcommaItems.Item
)
SELECT DISTINCT
UID,
employeeID,
STUFF(
(
SELECT
',' + stuff1.type
FROM
cte AS stuff1
WHERE
t.UID = stuff1.UID
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 1, ''
) AS userPermissions,
STUFF(
(
SELECT
',' + stuff2.type
FROM
cte AS stuff2
WHERE
t.UID = stuff2.UID
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 1, ''
) AS pagePermissions
FROM
cte AS t
The userTbl table looks like this:
------------------------------------------------
ID | employeeID | userPerms | pagePermPAges
------------------------------------------------
15 | 3FdFieu9I | 1,3 | 1,4,5,6
The userPermissions table looks like this:
----------------
ID | type
----------------
1 | Read
2 | Write
3 | Upload
4 | Admin
ID | name
----------------
1 | bindex
2 | flight
3 | submit
4 | form
5 | information
6 | myPage
7 | register
My current output of the query above is this:
-------------------------------------------------------------------------------------------------------
ID | employeeID | userPermissions | pagePermissions
-------------------------------------------------------------------------------------------------------
15 | 3FdFieu9I | Read,Read,Read...upload,upload,upload...| Read,Read,Read...upload,upload,upload...
It should read this though:
-------------------------------------------------------------------------
ID | employeeID | userPermissions | pagePermissions
-------------------------------------------------------------------------
15 | 3FdFieu9I | Read,Upload | bindex,form,information,myPage
Any MS SQL guru out there that can help me out?
Perhaps this may help
Example
Select A.ID
,A.employeeID
,usersPermissions = Stuff((Select Distinct ',' +[type] From userPermissions Where charindex(concat(',',ID,','),concat(',',A.[userPerms] ,','))>0 For XML Path ('')),1,1,'')
,pagePermissions = Stuff((Select Distinct ',' +[name] From pagePermissions Where charindex(concat(',',ID,','),concat(',',A.[userPermPages],','))>0 For XML Path ('')),1,1,'')
From usersTbl A
Returns