Sum with Isnull

  • Hello,

    I am trying to add multiple row data with ISnull in but I couldn't seem to make it work. My query is like below:

    SELECT QueryID, DatasetID, ISNULL (SUM(mm.XMLResults.value ('(./MM)[1]','varchar(100)'),(mm.XMLResults.value ('(./MM)[1]','varchar(100)'))),0)

    AS MEDICAL_MM from OVT_RESULTS CROSS APPLY XMLResults.nodes('/row[TITLE = "MM"]') as mm(XMLResults) where QueryID = 105

    I know this is very wrong.. I'm new to SQL. thanks so much in advance.

  • Does this gets you what you need?

    SELECT

    QueryID,

    DatasetID,

    ISNULL (SUM(mm.XMLResults.value ('(./MM)[1]','varchar(100)')),0) AS MEDICAL_MM

    FROM OVT_RESULTS

    CROSS APPLY XMLResults.nodes('/row[TITLE = "MM"]') as mm(XMLResults)

    WHERE QueryID = 105

    EDIT: OMG, I just saw that you're converting to varchar. Use a numeric data type instead which is appropriate to your values.

    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
  • As a general rule, it's better to put IsNulls inside Sums rather than outside. This avoids the warning message about NULL values being eliminated:

    Select Sum(IsNull([Col],0))

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil Parkin (4/26/2016)


    As a general rule, it's better to put IsNulls inside Sums rather than outside. This avoids the warning message about NULL values being eliminated:

    Select Sum(IsNull([Col],0))

    +1. Agree with Phil.

  • Steve Jones - SSC Editor (4/26/2016)


    Phil Parkin (4/26/2016)


    As a general rule, it's better to put IsNulls inside Sums rather than outside. This avoids the warning message about NULL values being eliminated:

    Select Sum(IsNull([Col],0))

    +1. Agree with Phil.

    But isn't that the same as saying Select Sum(Col) except in the case where all values of Col are NULL, and so you may just as well say Select ISNULL(SUM(Col),0) OK, so you get the warning message, but don't you save on some processor ticks, especially if there are a lot of rows?

    John

  • John Mitchell-245523 (4/26/2016)


    Steve Jones - SSC Editor (4/26/2016)


    Phil Parkin (4/26/2016)


    As a general rule, it's better to put IsNulls inside Sums rather than outside. This avoids the warning message about NULL values being eliminated:

    Select Sum(IsNull([Col],0))

    +1. Agree with Phil.

    But isn't that the same as saying Select Sum(Col) except in the case where all values of Col are NULL, and so you may just as well say Select ISNULL(SUM(Col),0) OK, so you get the warning message, but don't you save on some processor ticks, especially if there are a lot of rows?

    John

    Seems sensible, but I remember testing this once, and they were always very, very close, with the surprising twist that the SUM(ISNULL(... was typically the one with oh-so-slightly lower CPU and duration.

    I'll see if I can whip up a test later to see if my memory's deceiving me. 🙂

    Cheers!

  • Jacob Wilkins (4/26/2016)


    John Mitchell-245523 (4/26/2016)


    Steve Jones - SSC Editor (4/26/2016)


    Phil Parkin (4/26/2016)


    As a general rule, it's better to put IsNulls inside Sums rather than outside. This avoids the warning message about NULL values being eliminated:

    Select Sum(IsNull([Col],0))

    +1. Agree with Phil.

    But isn't that the same as saying Select Sum(Col) except in the case where all values of Col are NULL, and so you may just as well say Select ISNULL(SUM(Col),0) OK, so you get the warning message, but don't you save on some processor ticks, especially if there are a lot of rows?

    John

    Seems sensible, but I remember testing this once, and they were always very, very close, with the surprising twist that the SUM(ISNULL(... was typically the one with oh-so-slightly lower CPU and duration.

    I'll see if I can whip up a test later to see if my memory's deceiving me. 🙂

    Cheers!

    I did a test earlier today with similar results. I discarded it as I thought it wouldn't add much value.

    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
  • Steve Jones - SSC Editor (4/26/2016)


    Phil Parkin (4/26/2016)


    As a general rule, it's better to put IsNulls inside Sums rather than outside. This avoids the warning message about NULL values being eliminated:

    Select Sum(IsNull([Col],0))

    +1. Agree with Phil.

    +1. Agree with Phil and Steve



    Alvin Ramard
    Memphis PASS Chapter[/url]

    All my SSC forum answers come with a money back guarantee. If you didn't like the answer then I'll gladly refund what you paid for it.

    For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]

  • Hello Everyone,

    Thank you so much for all of your inputs. I managed to make the query work but doing this:

    SELECT QueryID, DatasetID, SUM(MEDICAL_MM) FROM

    (SELECT QueryID, DatasetID, convert (numeric,ISNULL (mm.XMLResults.value ('(./MM)[1]','varchar(100)'),mm.XMLResults.value ('(./MM)[1]','varchar(100)')))

    AS MEDICAL_MM, ISNULL (mm.XMLResults.value ('(./TPERIOD)[1]','varchar(100)'),mm.XMLResults.value ('(./TPERIOD)[1]','varchar(100)'))AS T_PERIOD

    from OVT_RESULTS CROSS APPLY XMLResults.nodes('/row[TITLE = "MM"]') as mm(XMLResults) where QueryID = 105) A

    group by QueryID, DatasetID

    Thanks again.. 🙂

Viewing 9 posts - 1 through 8 (of 8 total)

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