我有一个看起来像这样的表:
我要做的是根据相同的ResourceName和Permissions合并组,并为它们提供相同的唯一标识符。但是,我有多个组重复自己的多个ResourceNames,并且无法为同一组提供另一个唯一ID。
例如,GroupAdmin和GroupUser1将是一个具有ID和GroupUser2的集合,而GroupUser3将是另一个集合。我有这样的数百万行。
请帮我写一个查询或一些代码来解决这个问题。谢谢!
编辑添加了没有图片的表格
ResourceName Permissions GroupName
Resource1 Modify GroupAdmin
Resource1 Modify GroupUser1
Resource1 Read & Execute GroupUser2
Resource1 Read & Execute GroupUser3
Resource2 Modify GroupAdmin
Resource2 Modify GroupUser1
Resource2 Read & Execute GroupUser2
Resource2 Read & Execute GroupUser3
Resource3 Modify GroupAdmin
Resource3 Modify GroupUser1
Resource3 Read & Execute GroupUser2
Resource3 Read & Execute GroupUser3
Resource4 Modify GroupAdmin
Resource4 Modify GroupUser1
Resource4 Read & Execute GroupUser2
Resource4 Read & Execute GroupUser3
答案 0 :(得分:1)
select r + '-' + p as id, r, p, g from rpg;
将以非常简单的方式为您提供一个ID,基于相同的ResourceName r和Permissions p。
或者你可以这样做:
select r, p, g, rank() over(order by r, p) as id from rpg;
答案 1 :(得分:0)
建议,下次有助于添加所需结果的表格。话虽如此,我想我想出了你想要的东西。
试试这个:
--Create A Simple Version of YourTable
SELECT * INTO YourTable
FROM
(
SELECT 'Resource1' AS ResourceName, 'Modify' AS [Permissions], 'GroupAdmin' AS GroupName
UNION ALL
SELECT 'Resource1', 'Modify', 'GroupUser1'
UNION ALL
SELECT 'Resource1', 'Read & Execute', 'GroupUser2'
UNION ALL
SELECT 'Resource1', 'Read & Execute', 'GroupUser3'
) A
SELECT A.ID,
B.GroupName
FROM
(
--Groups Resource and Permssion combinations and assigns ID using row_number()
SELECT ROW_NUMBER() OVER (ORDER BY ResourceName,[Permissions]) ID,
ResourceName,[Permissions]
FROM YourTable
GROUP BY ResourceName,[Permissions]
) A
--Match up your GroupNames to your Resource/Permssion Pairs
INNER JOIN YourTable B
ON A.ResourceName = B.ResourceName
AND A.[Permissions] = B.[Permissions]
结果:
ID GroupName
-------------------- ----------
1 GroupAdmin
1 GroupUser1
2 GroupUser2
2 GroupUser3