3

If I have a team table with a unknown amount of members, is there a way to make the pivot query dynamic?

    create table #t (
    team varchar (20), member varchar (20)
)
insert into #t values ('ERP', 'Jack')
insert into #t values ('ERP', 'John')
insert into #t values ('ERP', 'Mary')
insert into #t values ('ERP', 'Tim')
insert into #t values ('CRM', 'Robert')
insert into #t values ('CRM', 'Diana')

select * from #t

select team, [1] as teamMember1,    /* 1st select */
    [2] as teamMember2, [3] as teamMember3
 from 
(select team , member, row_number ()    /* 3rd select */
    over (partition by team order by team) as rownum
from #t) a
pivot (max(member) for rownum in ([1], [2], [3])) as pvt

drop table #t
Bramble
  • 1,395
  • 13
  • 39
  • 55
  • 3
    I think you are looking for this: http://stackoverflow.com/questions/2922797/t-sql-pivot-possibility-of-creating-table-columns-from-row-values. Itzik Ben-Gan's example on how to build dynamic PIVOT. – Guy Nethery Feb 17 '14 at 18:09

1 Answers1

0

Why yes, yes there is. Here's a script I cooked up years ago for a similar problem that was ultimately solved by giving the user Excel and washing my hands of it. I apologize it's not configured with your example data, but hopefully it's easy to follow.

Hope that helps,

John

--------------START QUERY--------------
-- Example Table
CREATE TABLE #glbTestTable
(
    ProviderID  INT,
    Total       INT,
    PaymentDate SMALLDATETIME
)

--So the dates insert properly
SET DATEFORMAT dmy

-- Populate Example Table
INSERT INTO #glbTestTable VALUES (232, 12200, '12/01/09') 
INSERT INTO #glbTestTable VALUES (456, 10200, '12/01/09') 
INSERT INTO #glbTestTable VALUES (563, 11899, '02/03/09') 
INSERT INTO #glbTestTable VALUES (221, 5239, '13/04/09') 
INSERT INTO #glbTestTable VALUES (987, 7899, '02/03/09') 
INSERT INTO #glbTestTable VALUES (1, 1234, '02/08/09') 
INSERT INTO #glbTestTable VALUES (2, 4321, '02/07/09') 
INSERT INTO #glbTestTable VALUES (3, 5555, '02/06/09')

-- Raw Output
SELECT *
FROM #glbTestTable

-- Build Query for Pivot --
DECLARE @pvtColumns VARCHAR(MAX)
SET @pvtColumns = ''

-- Grab up to the first 1023 "Columns" that we want to use in Pivot Table.
--  Tables can only have 1024 columns at a maximum
SELECT TOP 1023 @pvtColumns = @pvtColumns + '[' + CONVERT(VARCHAR, PaymentDate, 103) + '], '
FROM (SELECT DISTINCT PaymentDate FROM #glbTestTable) t_distFP

-- Create PivotTable Query
DECLARE @myQuery VARCHAR(MAX)
SET @myQuery = '
SELECT ProviderID, ' + LEFT(@pvtColumns, LEN(@pvtColumns) - 1) + '
FROM (SELECT ProviderID, PaymentDate, Total
    FROM #glbTestTable) AS SourceTable
PIVOT
(
    SUM(Total)
    FOR PaymentDate IN (' + LEFT(@pvtColumns, LEN(@pvtColumns) - 1) + ')
) AS PivotTable'

-- Run the Pivot Query
EXEC(@myQuery)

-- Cleanup
DROP TABLE #glbTestTable
---------------END QUERY---------------
John Eisbrener
  • 642
  • 8
  • 17