Skip to content

Instantly share code, notes, and snippets.

@nathany
Created August 26, 2009 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathany/175670 to your computer and use it in GitHub Desktop.
Save nathany/175670 to your computer and use it in GitHub Desktop.
pivot table example in SQL Server 2005
select PT.FacID, f1.FacName, [1] as QtyAlloc, [-1] as QtyUsed, [1] + [-1] as QtyRemain
from
(
select f1.FacID, sign(f1.Quantity) as Sgn, f1.Quantity
from FacCredits f1
) as q1
pivot
(
sum(q1.Quantity)
for q1.Sgn in ([1], [-1])
) as PT
left join Facilitators f1 on PT.FacID = f1.FacID
order by [1] + [-1] desc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment