数据透视表数据

时间:2016-12-16 06:47:06

标签: sql pivot pivot-table

我有以下数据

  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

4 个答案:

答案 0 :(得分: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 ('+@DynamicCol+')
)pvt'

EXEC (@DynamicPvt)

答案 1 :(得分: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);

答案 2 :(得分: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

答案 3 :(得分: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)

这将带来渴望的结果,也是动态的。您可以拥有多项权利。