Adding Zero

  • HI,

    I have the below query set up. What i am trying to do is create a line that populates as value to be zero if there is no count of unit in a given month like below. Is this possible?

    There are 4 units, so for each month i want 4 lines, but currently if there are no actuals in a month its giving 3 lines.

    SELECT

    count([Unit]) as Actual,

    unit,

    [1st_of_month],

    last_of_month

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

    where RfR1 =

    '18 month project'

    group by unit,

    [1st_of_month],

    [last_of_month]

    Results

    6 NW 2013-08-01 2013-08-31

    4 SE 2013-08-01 2013-08-31

    5 SW 2013-08-01 2013-08-31

    Required Result

    6 NW 2013-08-01 2013-08-31

    4 SE 2013-08-01 2013-08-31

    5 SW 2013-08-01 2013-08-31

    0 NE 2013-08-01 2013-08-31

  • 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]

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Something like this would work:

    With units AS

    (

    Select Distinct

    unit

    FROM

    dbo.Full_Referrals_Dataset_live

    )

    SELECT

    count(FR.[Unit]) as Actual,

    U.unit,

    FR.[1st_of_month],

    FR.[last_of_month]

    FROM

    units as U LEFT JOIN

    dbo.Full_Referrals_Dataset_live as FR

    WHERE

    FR.RfR1 = '18 month project'

    GROUP BY

    U.unit,

    FR.[1st_of_month],

    FR.[last_of_month];

  • 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

  • Here is what I propose:

    WITH StartingPoint AS

    (

    --SELECT

    --count([Unit]) as Actual,

    --unit,

    --[1st_of_month],

    --last_of_month

    --FROM [lchs_live].[dbo].[Full_Referrals_Dataset_live]

    --where RfR1 =

    --'18 month project'

    --group by unit,

    --[1st_of_month],

    --[last_of_month]

    SELECT Actual=6,unit='NW',[1st_of_month]='2013-08-01',last_of_month='2013-08-31'

    UNION ALL SELECT 4,'SE','2013-08-01','2013-08-31'

    UNION ALL SELECT 5,'SW','2013-08-01','2013-08-31'

    ),

    Units (unit) AS

    (

    SELECT 'NW' UNION ALL SELECT 'SE' UNION ALL SELECT 'SW' UNION ALL SELECT 'NE'

    )

    SELECT Actual=ISNULL(Actual, 0), b.unit, a.[1st_of_month], a.last_of_month

    FROM

    (

    SELECT DISTINCT [1st_of_month], last_of_month

    FROM StartingPoint a

    ) a

    CROSS JOIN Units b

    LEFT JOIN StartingPoint c ON b.unit = c.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

  • hi all,

    many thanks for your assistance so far. I have created a units table as mentioned called lchs_ref.dbo.FHL_Units. I have tried to replicate what Jack Corbett has suggested but i keep coming up with an error. Here is my adapted query and the error i get. Any suggestions on what i am missing? I have never used the top part of the query before so apologies if its a simple error.

    With Units AS

    (

    Select Distinct

    unit

    FROM

    dbo.Full_Referrals_Dataset_live

    )

    SELECT

    count(FR.[Unit]) as Actual,

    U.unit,

    FR.[1st_of_month],

    FR.[last_of_month]

    FROM

    lchs_ref.dbo.FHL_Units as U

    LEFT JOIN

    dbo.Full_Referrals_Dataset_live as FR

    WHERE

    FR.RfR1 = '18 month project'

    GROUP BY

    U.unit,

    FR.[1st_of_month],

    FR.[last_of_month];

    Msg 156, Level 15, State 1, Line 18

    Incorrect syntax near the keyword 'WHERE'.

  • james.ingamells (10/10/2013)


    hi all,

    many thanks for your assistance so far. I have created a units table as mentioned called lchs_ref.dbo.FHL_Units. I have tried to replicate what Jack Corbett has suggested but i keep coming up with an error. Here is my adapted query and the error i get. Any suggestions on what i am missing? I have never used the top part of the query before so apologies if its a simple error.

    With Units AS

    (

    Select Distinct

    unit

    FROM

    dbo.Full_Referrals_Dataset_live

    )

    SELECT

    count(FR.[Unit]) as Actual,

    U.unit,

    FR.[1st_of_month],

    FR.[last_of_month]

    FROM

    lchs_ref.dbo.FHL_Units as U

    LEFT JOIN

    dbo.Full_Referrals_Dataset_live as FR

    WHERE

    FR.RfR1 = '18 month project'

    GROUP BY

    U.unit,

    FR.[1st_of_month],

    FR.[last_of_month];

    Msg 156, Level 15, State 1, Line 18

    Incorrect syntax near the keyword 'WHERE'.

    Oops. My bad. I forgot the ON clause for the JOIN. You need to add ON U.unit = FR.unit before the WHERE.

  • THanks Jack,

    It now runs, but does not pull the zero values i was hoping for. Do i need to join a calendar table on to it?

  • dwain.c (10/9/2013)


    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.

    You're absolutely right, I didn't think the query throughly and got distracted. :hehe:

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • james.ingamells (10/10/2013)


    THanks Jack,

    It now runs, but does not pull the zero values i was hoping for. Do i need to join a calendar table on to it?

    Guess you didn't like my try?


    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

  • Hi Dwain,

    I did have a go at your attempt, however if I want to be able to use this on a monthly basis i would have to do the manual insert statement every time??

    James

  • james.ingamells (10/11/2013)


    Hi Dwain,

    I did have a go at your attempt, however if I want to be able to use this on a monthly basis i would have to do the manual insert statement every time??

    James

    Not sure what you mean by the manual insert as mine doesn't do any inserts.

    I took your original code and embedded it in the first CTE. You should uncomment that and use it instead of the SELECT/UNION ALL SELECT query inside that CTE (you said that's what your query produced).

    The rest should stand pretty much on its own.


    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

  • HI Dwain,

    Sorry to be a pain, but i am struggling to make this work and i know its probably quite simple. Here is what i have changed and the results attached it produces:

    WITH StartingPoint AS

    (

    SELECT

    count([Unit]) as Actual,

    unit,

    [1st_of_month],

    last_of_month

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

    where RfR1 =

    '18 month project'

    group by unit,

    [1st_of_month],

    [last_of_month]

    --union all

    --SELECT Actual=6,unit='NW',[1st_of_month]='2013-08-01',last_of_month='2013-08-31'

    --UNION ALL SELECT 4,'SE','2013-08-01','2013-08-31'

    --UNION ALL SELECT 5,'SW','2013-08-01','2013-08-31'

    ),

    Units (unit) AS

    (

    SELECT 'NW' UNION ALL SELECT 'SE' UNION ALL SELECT 'SW' UNION ALL SELECT 'NE'

    )

    SELECT Actual=ISNULL(Actual, 0), b.unit, a.[1st_of_month], a.last_of_month

    FROM

    (

    SELECT DISTINCT [1st_of_month], last_of_month

    FROM StartingPoint a

    ) a

    CROSS JOIN Units b

    LEFT JOIN StartingPoint c ON b.unit = c.unit

  • That looks like my query but I don't see any results attached.

    BTW. There is the possibility that mine might not return what you expect if your base query returns more projects than in your sample data.


    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

Viewing 14 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic. Login to reply