I have a pivot table, but my issue is, once a year, it needs manually modifying and it shouldnt have to be that way.
What i currently have is:
SELECT * FROM (SELECT Injuries.PlayerID, InjuryID, YEAR(DateInjured) AS YearInjured, (YEAR(CURRENT_TIMESTAMP) - YEAR(DateOfBirth)) AS Age FROM Injuries INNER JOIN Players ON (Players.PlayerID = Injuries.PlayerID)) AS T
PIVOT (
COUNT(InjuryID)
FOR YearInjured IN ([2008],[2009],[2010],[2011],[2012],[2013],[2014],[2015])
) AS PT
I have tried
SELECT * FROM (SELECT Injuries.PlayerID, InjuryID, YEAR(DateInjured) AS YearInjured, (YEAR(CURRENT_TIMESTAMP) - YEAR(DateOfBirth)) AS Age FROM Injuries INNER JOIN Players ON (Players.PlayerID = Injuries.PlayerID)) AS T
PIVOT (
COUNT(InjuryID)
FOR YearInjured IN (SELECT DISTICT YEAR(DateInjured) FROM Injuries)
) AS PT
But SQL server does not like me doing that. I have also attempted to DECLARE
a table and select the years into that and use that within the PIVOT, but that does not seem to work too.
Is there a way, without being forced to use sp_exec
and a generated query, to do what i am trying to do?