2016-12-16 228 views
1

我有以下數據透視表數據

NAME | RIGHTS | 
Steven | add | 
Steven | update | 
Steven | delete | 
Mark | update | 
Mark | delete | 
Joseph | don’t have Rights | 
Spike | add | 
Spike | update | 
Spike | delete | 

而這個數據,我想操縱作爲

NAMEs | don’t have Rights | add| update | delete | 

Steven | 0 |1|1|1| 

Mark |0|0|1|1| 

Joseph |1|0|0|0| 

Spike |0|1|1|1| 

請注意,我沒有任何類型的權利的想法,也可以是上述100

+0

這是任何特定的數據庫? SQL Server? MySQL的?等等? –

+0

使用SQL Server – SAURABH

+0

Saurabh,Name&Rights是兩個不同的列? –

回答

1

我得到了答案

DECLARE @cols AS NVARCHAR(MAX), 
    @query AS NVARCHAR(MAX) 

select @cols = STUFF((SELECT distinct ',[' + Rights +']' 
        from MyTableName 
      FOR XML PATH(''), TYPE 
      ).value('.', 'NVARCHAR(MAX)') 
     ,1,1,'') 



set @query = 'SELECT Name,' + @cols + ' from 
      (
       SELECT Name, Rights,1 as xCount FROM MyTableName 
      ) x 
      pivot 
      (
       count(xCount) 
       for Rights in (' + @cols + ') 
      ) p ' 

execute(@query); 
1
**Using pivot to get that result :** 

CREATE TABLE #details(Id INT,NAME VARCHAR(100), RIGHTS VARCHAR(100)) 
INSERT INTO #details(Id ,NAME , RIGHTS) 
SELECT 1,'Steven','add' UNION ALL 
SELECT 1,'Steven','update' UNION ALL 
SELECT 1,'Steven','delete' UNION ALL 
SELECT 1,'Mark','update' UNION ALL 
SELECT 1,'Mark','delete' UNION ALL 
SELECT 1,'Joseph','don’t have Rights' UNION ALL 
SELECT 1,'Spike','add' UNION ALL 
SELECT 1,'Spike','update' UNION ALL 
SELECT 1,'Spike','delete' 

SELECT * 
FROM 
(
    SELECT NAME , RIGHTS ,Id 
    FROM #details 
)A 
PIVOT 
(
    MAX(Id) FOR RIGHTS IN ([don’t have Rights],[add],[update],[delete]) 
)pvt 

**For Dynamic pivot use below query :** 

DECLARE @DynamicCol VARCHAR(MAX) = '',@DynamicPvt VARCHAR(MAX) = '' 

SELECT @DynamicCol = 
(
    SELECT STUFF ((SELECT DISTINCT ',' + '[' + RTRIM(RIGHTS) + ']' FROM  
    #details FOR XML PATH('')),1,1,'') 
) 

SET @DynamicPvt = 'SELECT * 
FROM 
(
SELECT NAME , RIGHTS ,Id 
FROM #details 
)A 
PIVOT 
(
    MAX(Id) FOR RIGHTS IN ('[email protected]+') 
)pvt' 

EXEC (@DynamicPvt) 
+0

Mansoor,謝謝你的回答是正確的,但正如我告訴我,我不知道確切的權利數,這個問題我已經解決了我的答案。 – SAURABH

+0

使用動態數據透視權獲得'n'個權利 – Mansoor

0

選中此項。

  select NAME, 
      case when [don’t have Rights] is not null then 1 else 0 end as [don’t have Rights] , 
      case when [add] is not null then 1 else 0 end as [add] , 
      case when [update] is not null then 1 else 0 end as [update] , 
      case when [delete] is not null then 1 else 0 end as [delete] 
      from 
      (
      select * 
      from #YourTAble 
      pivot 
      (
      max(RIGHTS) for RIGHTS in ([don’t have Rights],[delete],[update],[add]) 
      )a 
      )b 

輸出:

enter image description here

0
Declare @Str varchar(max) 
, @FinalStr varchar(max) 
Set @Str='' 
Select @Str = @Str + ',[' + Rights+']' 
FROM (Select Distinct Rights FROM F2) A 
Set @Str= Substring(@str,2,LEN(@Str)) 

SET @FinalStr = 
'Select Name, ' + @Str + 
'FROM F2 
PIVOT (Count(Rights)For Rights in (' + @Str + ')) As A' 

Exec (@FinalStr) 

這將給慾望的結果,它是動態的一樣。你可以擁有一些權利。