+ Reply to Thread
Results 1 to 3 of 3

Thread: Including SQL Function in WHERE Clause

  1. #1
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    Including SQL Function in WHERE Clause

    Currently, I am trying to select only those records whose SUM is greater than 0:

    Code:
    	strSQL = "SELECT tblStocks_detail.s_id, tblStocks.s_name, tblStocks.s_symbol, " & _
    		"SUM(tblStocks_detail.sd_commission) As sd_commission, " & _
    		"SUM(tblStocks_detail.sd_shares) As sd_shares, " & _
    		"COUNT(tblStocks_detail.s_id) As sd_records, " & _
    		"(SUM(tblStocks_detail.sd_shares * tblStocks_detail.sd_price_per_share) / SUM(tblStocks_detail.sd_shares)) As sd_price_per_share " & _
    		"FROM tblStocks " & _
    		"INNER JOIN tblStocks_detail ON tblStocks.s_id = tblStocks_detail.s_id " & _
    		"WHERE SUM(tblStocks_detail.sd_shares) > 0 " & _
    		"GROUP BY tblStocks_detail.s_id, tblStocks.s_name, tblStocks.s_symbol " & _
    		"ORDER BY tblStocks.s_name"
    
    However, this is generating the following error:

    Cannot have aggregate function in WHERE clause (SUM(tblStocks_detail.sd_shares)>0).
    What is the proper syntax? Reason being is that in my table, I might have some stock that have been sold. The way I show they have been sold, is to create a new record with the negative of the total.

    For instance, if you owned 50 shares of a stock and sold them, you would have one record with positive 50 shares, and another with negative 50 shares. I do not want to show these records in my results.
    "You'll never be as perfect as BLaaaaaaaaarche."

  2. #2
    Wolfmaster Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy is a splendid one to behold Wolffy's Avatar
    Join Date
    Mar 2008
    Location
    Peoria, IL
    Posts
    2,386
    Blog Entries
    5
    Real Name
    Wolff
    Rep Power
    15

    After the GROUP BY clause put:
    HAVING SUM(tblStocks_detail.sd_shares) > 0
    Wolffy
    .-- ----- ..-. ..-. -.--
    Opinions expressed are my own and do not necessity reflect those of any sane person. Any code provided is intended to be an example and is provided AS IS. Void where prohibited by law. Not valid in California. Your mileage may vary.

  3. #3
    Barn Frequenter BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche will become famous soon enough BLaaaaaaaaaarche's Avatar
    Join Date
    Mar 2008
    Posts
    188
    Rep Power
    5

    Well that was certainly easy enough. Thanks Wolffy.
    "You'll never be as perfect as BLaaaaaaaaarche."

+ Reply to Thread

Similar Threads

  1. Help with JS function
    By Wolffy in forum JavaScript Programming
    Replies: 1
    Last Post: June 9th, 2009, 10:52 AM
  2. Including a control in text returned from a WebMethod
    By richyrich in forum .NET Development
    Replies: 1
    Last Post: January 5th, 2009, 12:55 PM
  3. Help using function
    By Rebelle in forum ASP Development
    Replies: 11
    Last Post: October 13th, 2008, 01:13 PM
  4. without function
    By guddu in forum Microsoft SQL Server
    Replies: 1
    Last Post: July 15th, 2008, 05:02 PM
  5. Using a field value in an IN(...) clause
    By richyrich in forum MySQL
    Replies: 5
    Last Post: April 13th, 2008, 12:56 PM

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

SEO by vBSEO