Avatar of Leo Torres
Leo TorresFlag for United States of America asked on

2005 SQL Pivot

I am trying to pivot years but the years are not showing up
Here are the results from both Queries the second one just has first inside with pivot
 Datayears are missing

What did I do wrong or what am I missing
Microsoft SQL Server 2005Microsoft SQL Server 2008SQL

Avatar of undefined
Last Comment
Leo Torres

8/22/2022 - Mon
Brendt Hess

The problem is with the SELECT field list.  You need to include the individual years being reported on in the SELECT.  As shown in an example from BoL:

SELECT VendorID, [164] AS Emp1, [198] AS Emp2, [223] AS Emp3, [231] AS Emp4, [233] AS Emp5
FOR EmployeeID IN
( [164], [198], [223], [231], [233] )
) AS pvt

Open in new window

Leo Torres

Away from Computer with code but yes, I did include every combination on list I  will try to post later of tomorrow morning

Still did not work






from (

Select sr.SubmissionName

, sc.LineCount as ReportTotal

, isNULL(dos.FirstDOS,0) as FirstDOS

, isNull(dos.LastDOS,0) as LastDOS

, cast(isNull(s.[Year],0) as varchar(max))   as [Year]

, cast(isNull(s.LineCount,0) as varchar(max))  as YearlyCount

from dbo.Hedis_SubmissionReports sr

      join dbo.Hedis_SubmissionLineCounts sc

            on sr.ReportKey = sc.ReportKey

            and sc.FileID = 1

      Left Join dbo.Hedis_SubmissionDatesOService dos

            on dos.FileID = sc.FileID

                  and dos.ReportKey = sc.ReportKey

      Left Join dbo.Hedis_SubmissionCountsByYear s

            on s.FileID = dos.FileID

                  and s.ReportKey = dos.ReportKey    



pivot (MAX(p.[YearlyCount]) for p.[Year] in (








                                                                 , [2008]








                                                                  )  ) uPvt

order by SubmissionName

Open in new window


Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Leo Torres

Thank you!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.