2017-05-04 67 views
2

我正在嘗試在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期望的結果?

+4

這是用於表示層的工作。 – McNets

回答

2

在這裏,我們concatinate通過XML字符串和STUFF(),最後注入一個CRLF作爲分隔符

需要明確的是,在這種情況下數據的只有兩排將返回,但是每個可具有多條線。

Declare @YourTable Table ([ID] varchar(50),[Name] varchar(50),[Roles] varchar(50),[Groups] varchar(50),[Profiles] varchar(50)) 
Insert Into @YourTable Values 
(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') 

Select ID 
     ,Name 
     ,Roles = replace((Select Stuff((Select Distinct '|'+Roles From @YourTable Where ID=A.ID For XML Path ('')),1,1,'')),'|',char(13)+char(10)) 
     ,Groups = replace((Select Stuff((Select Distinct '|'+Groups From @YourTable Where ID=A.ID For XML Path ('')),1,1,'')),'|',char(13)+char(10)) 
     ,Profiles = replace((Select Stuff((Select Distinct '|'+Profiles From @YourTable Where ID=A.ID For XML Path ('')),1,1,'')),'|',char(13)+char(10)) 
From (
     Select Distinct ID,Name From @YourTable 
    ) A 

返回

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 
+0

謝謝!這正是我一直在尋找的東西。 – brandonstrong

+0

@brandonstrong快樂它幫助 –

1

它主要是在UI /表示層,但你可以通過使用ROW_NUMBER實現這一點,如果是1,你可以別的選擇,你可以選擇NULL如下:

select ID = case when(Row_Number() over (partition by id order by id) = 1) then id else null end 
    , Name = case when(row_number() over (partition by id order by id) = 1) then Name else null end 
    , Roles = case when (row_number() over (partition by id, roles order by id) = 1) then Roles else null end 
    , Groups = case when (row_number() over (partition by id, Groups order by id) = 1) then Groups else null end 
    , Profiles 
    from #yourreturnstable 
+0

我試圖用列名即ie替換'id'。 Name = case when(row_number()over(DWUser.uid order by DWUser.uid)= 1)then DWRoles.name else null end,並且輸出不具有所有正確的值並且順序不正確。用戶名稱列在左下方,角色,組和配置文件分散在整個表格中,沒有連貫的順序。我是否用錯誤的值替換了'id'? – brandonstrong

+0

使用ID,名稱分區名稱,ID,名稱,角色角色分區由和,ID,名稱,角色,組中的組分區...看起來像你的數據不同於示例數據顯示 –

+0

哦!對不起,我錯誤地閱讀了你的答案。好吧,所以我再次嘗試了它,它主要工作,但它只是取消了「堆疊」在另一個上的重複值。因此,例如,它不會取消「管理員」用戶的「個人資料」列中的任何重複值 – brandonstrong

相關問題