How to pivot in SQL

前端 未结 3 1478
無奈伤痛
無奈伤痛 2021-01-07 11:12

I am not sure if this would be called pivoting.

Data in my SQL 2005 table [CustromerRoles] is as such:

CustId  RoleId
2        4
2           


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 11:54

    PIVOT has the disadvantage that the columns must be known, because you have to provide the ids in the query. You can work around this by using dynamic SQL, i.e. generating the PIVOT query dynamically based on separate query results from the Roles table, in your case, then executing the result. This can easily be done in a stored procedure.

    Example:

    CREATE TABLE #CustomerRole ([CustId] int, [RoleId] int);
    INSERT INTO #CustomerRole values (2, 4);
    INSERT INTO #CustomerRole values (2, 3);
    INSERT INTO #CustomerRole values (3, 4);
    INSERT INTO #CustomerRole values (4, 1);
    INSERT INTO #CustomerRole values (4, 2);
    
    CREATE TABLE #Role ([Id] int, [Role] varchar(20));
    INSERT INTO #Role values (1, 'Admin');
    INSERT INTO #Role values (2, 'Manager');
    INSERT INTO #Role values (3, 'Support');
    INSERT INTO #Role values (4, 'Assistant');
    
    DECLARE @RoleList nvarchar(MAX)
    SELECT @RoleList = COALESCE(@RoleList + ',[' + [Role] + ']',
     '[' + [Role] + ']') 
        FROM #Role;
    
    DECLARE @SQL Nvarchar(max);
    SET @SQL = 'SELECT 
                    [CustId] '  + 
                    ISNULL(', ' + @RoleList , '') + ' 
                    FROM #CustomerRole custrole
                    inner join #Role as r
                    on r.[Id] = custrole.[RoleId]
                PIVOT (count([Id]) for [Role] IN 
                    (' + ISNULL(@RoleList, '[No role]') +
                     ')) as pvt;' 
    
    EXEC sp_executesql @SQL;
    
    drop table #Role;
    drop table #CustomerRole;
    

提交回复
热议问题