我正在嘗試在SSMS 2008中編寫SQL腳本,並且已經在網上尋找答案,我發現沒有什麼是完整的解決方案。SQL在列中選擇不同的值/列中的動態值計數
我有幾個關於分配給用戶和腳本的角色,組和配置文件的信息以獲取該信息。我得到的結果是「正確的」,但沒有以顧客希望看到的方式顯示。角色,組和配置文件彼此之間沒有任何關聯,並且可能會爲每個用戶分配不同數量的其他用戶(即2個角色,1個組和4個配置文件)。
這裏是我的SQL腳本:
select distinct
DWUser.uid as ID,
DWUser.name as Name,
DWRoles.name as Roles,
DWGroup.name as Groups,
DWFCProfile.name as Profiles
from DWUser
/* Joins for getting role name */
inner join DWUserToRole on DWUser.uid = DWUserToRole.uid
inner join DWRoles on DWUserToRole.rid = dwRoles.rid
/* Joins for getting group name */
inner join DWUserToGroup on DWUser.uid = DWUserToGroup.uid
inner join DWGroup on DWUserToGroup.gid = DWGroup.gid
/* Joins for getting profile name */
inner join DWFCProfileToUser on DWUser.uid = DWFCProfileToUser.uid
inner join DWFCProfile on DWFCProfileToUser.fpid = DWFCProfile.fpid
它返回這樣的結果:所以基本上
ID Name Roles Groups Profiles
1 admin Organization Administrator Public Contracts
System Administrator My Basket 1
My Basket 2
My Basket 3
4 docuware Organization Administrator Public Contracts
votosign My Basket 1
My Basket 2
My Basket 3
爲:
ID Name Roles Groups Profiles
1 admin Organization Administrator Public Contracts
1 admin Organization Administrator Public My Basket 1
1 admin Organization Administrator Public My Basket 2
1 admin Organization Administrator Public My Basket 3
1 admin System Administrator Public Contracts
1 admin System Administrator Public My Basket 1
1 admin System Administrator Public My Basket 2
1 admin System Administrator Public My Basket 3
4 docuware Organization Administrator Public Contracts
4 docuware Organization Administrator Public My Basket 1
4 docuware Organization Administrator Public My Basket 2
4 docuware Organization Administrator Public My Basket 3
4 docuware Organization Administrator votosign Contracts
4 docuware Organization Administrator votosign My Basket 1
4 docuware Organization Administrator votosign My Basket 2
4 docuware Organization Administrator votosign My Basket 3
客戶希望看到的結果的方式是每個用戶我需要刪除任何重複的角色,組和配置文件,而不管它們出現在列中的順序如何umn(即使重複項不像「角色」和「組」列中的「堆疊」)。
我敢肯定的答案是「否」,但這裏有雲:
有沒有辦法輸出使用Microsoft SQL期望的結果?
這是用於表示層的工作。 – McNets