+ Reply to Thread
Results 1 to 6 of 6

Thread: Help with query

  1. #1
    Registered User
    Join Date
    01-08-2009
    Location
    Colorado
    MS-Off Ver
    Excel 2007, 2010 PC, 2011 Mac
    Posts
    39

    Help with query

    I have very little knowledge of access, but knowledge of how databases in general function. I inherited this database and did not create it.

    I am using Access 2007.

    I have a report that is driven by a query. It is returning records that the sum is 0. I would like to not display the records where the sum is 0.
    I tried modifying the query in design view but I can't seem to get it to work. So I switched to the SQL view and have been trying to get that working. I can get access to accept the SQL but when I run it it asks me for the value that I put >0 in the query.

    Here is the SQL Query
    Code:
    SELECT LotNumber.LotNumber, MetalSizes.MetalSize, MetalSizes.Plating, MetalSizes.Thickness, Sum(LotAdjustment.Adjustment) AS SumOfAdjustment, MetalSizes.MetalID
    FROM (MetalSizes INNER JOIN LotNumber ON MetalSizes.MetalID = LotNumber.MetalID) INNER JOIN LotAdjustment ON LotNumber.LotNumber = LotAdjustment.LotNumber
    WHERE ((([SumOfAdjustment])>0))
    GROUP BY LotNumber.LotNumber, MetalSizes.MetalSize, MetalSizes.Plating, MetalSizes.Thickness, MetalSizes.MetalID
    ORDER BY LotNumber.LotNumber;
    With the code above it prompts me for the value of SumOfAdjustment. I am unsure of what I am doing wrong.

    In the design view I was able to get access to add a HAVING line in SQL. This did not ask for the value but it did not generate the correct results either.

    The end result would be showing just the records where SumOfAdjustment >0.



    Thanks

    Brian
    Last edited by extrapulp; 01-18-2010 at 10:11 AM. Reason: Reworded added more info

  2. #2
    Cheeky Forum Moderator Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2003, 2007, 2010
    Posts
    3,605

    Re: Help with query

    In the query SQL, you can't reference field aliases in the FROM, WHERE, GROUP BY, HAVING, or ORDER BY sections.

    Try this:

    Code:
    SELECT 
      LotNumber.LotNumber, 
      MetalSizes.MetalSize, 
      MetalSizes.Plating, 
      MetalSizes.Thickness, 
      Sum(LotAdjustment.Adjustment) AS SumOfAdjustment, 
      MetalSizes.MetalID
    FROM 
      (MetalSizes INNER JOIN LotNumber ON MetalSizes.MetalID = LotNumber.MetalID) 
      INNER JOIN LotAdjustment ON LotNumber.LotNumber = LotAdjustment.LotNumber
    WHERE (Sum(LotAdjustment.Adjustment)>0)
    GROUP BY 
      LotNumber.LotNumber, 
      MetalSizes.MetalSize, 
      MetalSizes.Plating, 
      MetalSizes.Thickness, 
      MetalSizes.MetalID
    ORDER BY LotNumber.LotNumber;

    Does that help?
    Regards,

    Ron
    Microsoft MVP - Excel
    (Oct 2006 - Sep 2012)

    Click here to see the Forum Rules

  3. #3
    Registered User
    Join Date
    01-08-2009
    Location
    Colorado
    MS-Off Ver
    Excel 2007, 2010 PC, 2011 Mac
    Posts
    39

    Re: Help with query

    Ron,

    Access now throws an error dialog with this text in it "Cannot have aggregate function in WHERE clause (Sum(LotAdjustment.Adjustment)>0)."

    Thanks for your quick reply!

    Brian

  4. #4
    Forum Moderator ConneXionLost's Avatar
    Join Date
    03-11-2009
    Location
    Victoria, Canada
    MS-Off Ver
    2003 & 2010
    Posts
    1,797

    Re: Help with query

    How about this:

    Code:
    SELECT LotNumber, MetalSize, Plating, Thickness, SumOfAdjustment, MetalID
    FROM (SELECT LotNumber.LotNumber, MetalSizes.MetalSize, MetalSizes.Plating, MetalSizes.Thickness, Sum(LotAdjustment.Adjustment) AS SumOfAdjustment, MetalSizes.MetalID
    FROM (MetalSizes INNER JOIN LotNumber ON MetalSizes.MetalID = LotNumber.MetalID) INNER JOIN LotAdjustment ON LotNumber.LotNumber = LotAdjustment.LotNumber;)
    WHERE ([SumOfAdjustment])>0
    GROUP BY LotNumber, MetalSize, Plating, Thickness, MetalID
    ORDER BY LotNumber;
    Cheers,
    Docendo discimus.

    Please consider:
    • Thanking those who helped you. Click the reputation icon in the contributor's post and add Reputation.
    • Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  5. #5
    Cheeky Forum Moderator Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2003, 2007, 2010
    Posts
    3,605

    Re: Help with query

    Quote Originally Posted by extrapulp View Post
    Ron,

    Access now throws an error dialog with this text in it "Cannot have aggregate function in WHERE clause (Sum(LotAdjustment.Adjustment)>0)."

    Thanks for your quick reply!

    Brian
    Try moving the criteria into a HAVING clause:

    Code:
    SELECT 
      LotNumber.LotNumber, 
      MetalSizes.MetalSize, 
      MetalSizes.Plating, 
      MetalSizes.Thickness, 
      Sum(LotAdjustment.Adjustment) AS SumOfAdjustment, 
      MetalSizes.MetalID
    FROM 
      (MetalSizes INNER JOIN LotNumber ON MetalSizes.MetalID = LotNumber.MetalID) 
      INNER JOIN LotAdjustment ON LotNumber.LotNumber = LotAdjustment.LotNumber
    GROUP BY 
      LotNumber.LotNumber, 
      MetalSizes.MetalSize, 
      MetalSizes.Plating, 
      MetalSizes.Thickness, 
      MetalSizes.MetalID
    HAVING 
      (Sum(LotAdjustment.Adjustment)>0)
    ORDER BY 
      LotNumber.LotNumber;
    Regards,

    Ron
    Microsoft MVP - Excel
    (Oct 2006 - Sep 2012)

    Click here to see the Forum Rules

  6. #6
    Registered User
    Join Date
    01-08-2009
    Location
    Colorado
    MS-Off Ver
    Excel 2007, 2010 PC, 2011 Mac
    Posts
    39

    Re: Help with query

    Ron,

    That works great!

    Thank you!

    Brian

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.2.0