+ Reply to Thread
Results 1 to 6 of 6

SQL syntax for SUM

  1. #1
    Registered User
    Join Date
    05-10-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    3

    SQL syntax for SUM

    I am using Excel VBA do write a SQL query using ADO.

    E.g. of my query:

    Select Item,Sum(BookValue) From ItemLedger Group By Item

    How do I modify my query such that those items with Sum(BookValue)=0 will not be retrieved?
    Last edited by NBVC; 05-06-2011 at 03:51 PM.

  2. #2
    Forum Expert ConneXionLost's Avatar
    Join Date
    03-11-2009
    Location
    Victoria, Canada
    MS-Off Ver
    2010
    Posts
    2,952

    Re: SQL syntax for SUM

    Include a HAVING option in your SQL statement.

    Cheers,
    Last edited by ConneXionLost; 05-10-2010 at 01:50 AM.
    Would you like to say thanks? Please click the: " Add Reputation" button, on the grey bar below the post.

  3. #3
    Registered User
    Join Date
    05-10-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: SQL syntax for SUM

    I have tried

    Select Item,Sum(BookValue) From ItemLedger Having Sum(BookValue)<>0 Group By Item

    But it returns an error.

  4. #4
    Forum Expert ConneXionLost's Avatar
    Join Date
    03-11-2009
    Location
    Victoria, Canada
    MS-Off Ver
    2010
    Posts
    2,952

    Re: SQL syntax for SUM

    This example SQL statement works:

    Please Login or Register  to view this content.
    If the same structure doesn't work for you, then perhaps it is the VBA you're using to run it.
    Last edited by ConneXionLost; 05-10-2010 at 02:08 AM.

  5. #5
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: SQL syntax for SUM

    Assuming you don't have negative BookValues, why not just add a:
    WHERE BookValue > 0
    clause?
    Please Login or Register  to view this content.
    Remember what the dormouse said
    Feed your head

  6. #6
    Registered User
    Join Date
    05-10-2010
    Location
    Singapore
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: SQL syntax for SUM

    Hi guys,

    Thanks for your help. I've got it working. I didn't know sequence of the syntax makes a difference and that GROUP BY must be before HAVING.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1