Results 1 to 7 of 7

Thread: [RESOLVED] Null result

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    122

    Resolved [RESOLVED] Null result

    Hello all,

    I'm trying run a query to do some calculation. If field2 and field3 have zero value it throws an exception. Can anyone kindly give some insight as how to work around.

    sum(1)*MAX(field2)/MAX(field3)

    thanks,

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Null result

    Most aggregate functions cannot deal with NULL - and NULL represents and infinite value and can not be "added" or "maxed" with other real values.

    If this is MS SQL use the ISNULL function to transform your null values.

    sum(1)*MAX(IsNull(field2,0))/MAX(IsNull(field3,0))

    If this is ACCESS then I think there is a NZ() function that does something similar. It's always helpful to tell us what DB you are using.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    122

    Re: Null result

    I'm trying to run the query in access database.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Null result

    Then lookup how to use NZ() - I'm not an access person but I believe that NZ() does the same thing as ISNULL().

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    122

    Re: Null result

    select sum(1) *MAX( Nz((table1.total),1))/MAX(Nz((table1.time),1)) as cal,machine from table1 group by field2

    tried Nz function, but i still get error as i'm dividing zero by zero in the calculation as i have a record where table1.total and table1.time has value 0.

    kindly give some insight.

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Null result

    First - I don't understand why you are using NZ() to turn the blank/null value into a 1 - shouldn't you be turning it into 0??

    Maybe something like this:

    select IIF(MAX(Nz(table1.time,0)) =0,0,sum(1) *MAX( Nz(table1.total,0))/MAX(Nz(table1.time,0)) )

    IIF() can test the divisor value to see if it's 0.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    122

    Resolved Re: Null result

    thanks.. it worked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width