• Thanks Lynn! Apparently if I could read, I would've come up with this version instead:

    ;WITH Unpivoted AS (

    SELECT SchoolCode, RaceCode, Code, Value

    FROM #Temp

    CROSS APPLY (

    VALUES ('LUNCH', Lunch), ('ESL', Student_ESL), ('GIEP', GIEP), ('IEP', IEP)

    ) a (Code, Value)

    )

    SELECT Sex=CASE Sex WHEN 'M' THEN 'MALE' ELSE 'FEMALE' END

    ,BLACK=COUNT(CASE WHEN RaceCode = 'Black' THEN 1 END)

    ,WHITE=COUNT(CASE WHEN RaceCode = 'White' THEN 1 END)

    ,OTHER=COUNT(CASE WHEN RaceCode = 'Other' THEN 1 END)

    FROM #Temp

    WHERE SchoolCode = 204

    GROUP BY Sex

    UNION ALL

    SELECT Code

    ,BLACK=SUM(CASE WHEN RaceCode = 'Black' THEN Value ELSE 0 END)

    ,WHITE=SUM(CASE WHEN RaceCode = 'White' THEN Value ELSE 0 END)

    ,OTHER=SUM(CASE WHEN RaceCode = 'Other' THEN Value ELSE 0 END)

    FROM Unpivoted

    WHERE SchoolCode = 204

    GROUP BY Code


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St