Harry Strybos
3/18/2007 9:57:00 PM
Hi All
I have constructed the following query from Sql 2005 help sample. Pivot
tables are new to me.
Essentially, I have data the looks like this:
BDMName DateCollected ActiveMembers
------------------------------------------------------------- -----------------------
-------------
David Connell 2006-10-31 00:00:00 33411
David Connell 2006-11-30 00:00:00 32169
David Connell 2006-12-31 00:00:00 31778
David Connell 2007-01-31 00:00:00 31565
David Connell 2007-02-28 00:00:00 31775
Scott Jackson 2006-10-31 00:00:00 13396
Scott Jackson 2006-11-30 00:00:00 13232
Scott Jackson 2006-12-31 00:00:00 12287
Scott Jackson 2007-01-31 00:00:00 12234
Scott Jackson 2007-02-28 00:00:00 12099
Paul Richardson 2006-10-31 00:00:00 27613
Paul Richardson 2006-11-30 00:00:00 27354
Paul Richardson 2006-12-31 00:00:00 26217
Paul Richardson 2007-01-31 00:00:00 26488
Paul Richardson 2007-02-28 00:00:00 26628
--example only, aliases will be created in code
SELECT Salesperson, [1] As 'Jun', [2] AS 'Jul', [3] AS 'Aug', [4] AS 'Sep',
[5] AS 'Oct'
FROM
(SELECT RTRIM(B.FirstName) + ' ' + B.LastName AS Salesperson,
S.DateCollected,
S.ActiveMembers AS Customers
FROM dbo.BDMStat AS S
INNER JOIN dbo.BDM AS B ON S.BDM_ID = B.ID
WHERE S.DateCollected IN
(SELECT DISTINCT Top(5) DateCollected FROM dbo.BDMStat
ORDER BY DateCollected DESC) AS SourceTable
PIVOT
(
ActiveMembers
FOR DateCollected IN ([1], [2], [3], [4], [5]) ----- error here "Incorrect
syntax near the keyword 'FOR'"
) AS PivotTable
I want the data to look like this:
Salesperson, Date1, Date2, Date3, Date4, Date5
David Connel, 33411, 32169, 31778 etc.............
Thanks for any help