• Luis Cazares (10/9/2013)


    You need a Units table or at least a Units set to look for all units. after that the rest is easy, just add a RIGHT JOIN.

    SELECT

    count(fr.[Unit]) as Actual,

    fr.unit,

    [1st_of_month],

    last_of_month

    FROM [lchs_live].[dbo].[Full_Referrals_Dataset_live] fr

    RIGHT JOIN Units u ON fr.unit = u.unit

    where RfR1 = '18 month project'

    group by fr.unit,

    [1st_of_month],

    [last_of_month]

    Uh, Luis. Isn't that RIGHT JOIN going to leave you with NULLs in the resulting [1st_of_month], last_of_month columns? And also I think you'd want to refer to u.unit and not fr.unit.


    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