嘿所有我有以下SQL查询,我正在使用以从单元格中获取逗号分隔值:
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
userTbl 表格如下所示:
------------------------------------------------
ID | employeeID | userPerms | pagePermPAges
------------------------------------------------
15 | 3FdFieu9I | 1,3 | 1,4,5,6
userPermissions 表如下所示:
----------------
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
上面查询的当前输出是:
-------------------------------------------------------------------------------------------------------
ID | employeeID | userPermissions | pagePermissions
-------------------------------------------------------------------------------------------------------
15 | 3FdFieu9I | Read,Read,Read...upload,upload,upload...| Read,Read,Read...upload,upload,upload...
应该读取此内容:
-------------------------------------------------------------------------
ID | employeeID | userPermissions | pagePermissions
-------------------------------------------------------------------------
15 | 3FdFieu9I | Read,Upload | bindex,form,information,myPage
那里的任何MS SQL大师可以帮助我吗?
答案 0 :(得分:2)
也许这可能会有所帮助
示例强>
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
<强>返回强>
答案 1 :(得分:1)
您只需获取权限的唯一值即可。您可以通过DISTINCT
或GROUP BY
到达那里。这是使用前一种方法修改的代码:
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 DISTINCT
',' + stuff1.type
FROM
cte AS stuff1
WHERE
t.UID = stuff1.UID
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 1, ''
) AS userPermissions,
STUFF(
(
SELECT DISTINCT
',' + 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
答案 2 :(得分:0)
我认为应该这样做 如果你添加
Group by
在For XML
之前