Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727

    subquery error

    All, trying to create this subquery but I get an error: the select statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect. This is my code:



    Code:
    SELECT  tblCust.Custid, tblgross.[grossreceipts], Max(SumOfgrossreceipts) AS [MaxOfSumOfgross receipts]
    (SELECT tblCust.Custid, tblgross.grossreceipts, tblCust.Custid, Sum(tblgross.grossreceipts)AS [SumOfgrossreceipts]
    FROM tblgross LEFT JOIN tblCust ON tblgross.custid = tblCust.custid
    GROUP BY tblCust.Custid, tblgross.grossreceipts, tblCust.costid)
    FROM tblCust LEFT JOIN tblgross ON tblgross.custid= tblCust.custid;
    Can someone help please? Thanks

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    I'm not clear on your goal, but as you have it there would need to be a comma before the subquery. That said I don't think it will work. You realize you can get the Sum and Max in a single query?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    I was trying to keep from writing multiple queries. And I didn't think I could get the sum and max in one query. That was my ultimate goal was to write a single query getting the sum then max for the gross receipts. But could not get it to work which is why the subquery. Help please??

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Try this, though there are a couple of questionable things in there.


    SELECT tblCust.Custid, tblgross.[grossreceipts], Max(SumOfgrossreceipts) AS [MaxOfSumOfgross receipts],
    Sum(tblgross.grossreceipts) AS [SumOfgrossreceipts]
    FROM tblgross LEFT JOIN tblCust ON tblgross.custid = tblCust.custid
    GROUP BY tblCust.Custid, tblgross.grossreceipts
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    I get error: subqueries cannot be used in the expression (Max(SumOfgrossreceipts))
    What are the questionable things please?

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Is that the name of a field? Concerns include both grouping on and summing the same field and grouping on a field tblCust because of the LEFT JOIN.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    yes; I am trying to get the sum and max of gross receipts. I'm guessing this is not possible in one query?

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    I already said it was. I think you made up some field names. See if this simpler version works:

    SELECT Custid, Max(grossreceipts) AS [MaxOfgross receipts],
    Sum(grossreceipts) AS [SumOfgrossreceipts]
    FROM tblgross
    GROUP BY Custid
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    I apologize. I need to make sure I understand. First; I realized I put a space in MaxOfSumOfgross receipts where it should not be. You latest code leaves out the join?? So just so I understand, I can't get the sum and max of grossreceipts field in the same query using a join and grouping?

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    I didn't say that, I just wanted to start simple and then work from there. Did it work? I didn't see you getting a field from the other table anyway, other than the join field.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Oh. I misunderstood. Yes it did work. I need to include the join tblCust.Custid.

  12. #12
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Thanks so much Paul. I added back the join field by field and it worked fine. There is some difference in the expected results but I think I can resolve it at this point. I will work on this before closing out the thread. Thanks again!

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    Xipooo's Avatar
    Xipooo is offline Sr. Database Developer
    Windows 8 Access 2013
    Join Date
    Jan 2014
    Location
    Arizona
    Posts
    332
    I know I'm jumping in late here but in his original query he didn't alias his subquery and that's probably confusing the engine on where it's supposed to grab the data from.

    Code:
    SELECT  tblCust.Custid, qrygross.[grossreceipts], Max(SumOfgrossreceipts) AS [MaxOfSumOfgross receipts]
    (SELECT tblCust.Custid, tblgross.grossreceipts, tblCust.Custid, Sum(tblgross.grossreceipts)AS [SumOfgrossreceipts]
    FROM tblgross LEFT JOIN tblCust ON tblgross.custid = tblCust.custid
    GROUP BY tblCust.Custid, tblgross.grossreceipts, tblCust.costid) as qrygross
    FROM tblCust LEFT JOIN qrygross ON qrygross.custid= tblCust.custid;

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,530
    Quote Originally Posted by Xipooo View Post
    I know I'm jumping in late here but in his original query he didn't alias his subquery and that's probably confusing the engine on where it's supposed to grab the data from.

    Code:
    SELECT  tblCust.Custid, qrygross.[grossreceipts], Max(SumOfgrossreceipts) AS [MaxOfSumOfgross receipts]
    (SELECT tblCust.Custid, tblgross.grossreceipts, tblCust.Custid, Sum(tblgross.grossreceipts)AS [SumOfgrossreceipts]
    FROM tblgross LEFT JOIN tblCust ON tblgross.custid = tblCust.custid
    GROUP BY tblCust.Custid, tblgross.grossreceipts, tblCust.costid) as qrygross
    FROM tblCust LEFT JOIN qrygross ON qrygross.custid= tblCust.custid;
    True, but I don't think the original subquery would have worked anyway, with multiple fields in the SELECT and the fact that it could return multiple records. It could have worked if it were joined in the FROM clause, but wasn't necessary in either case. I don't think you can join to it the way you have it (it would need to be in the FROM clause, not the SELECT clause). Plus you've left out the comma too.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Subquery
    By Daryl2106 in forum Access
    Replies: 20
    Last Post: 01-13-2013, 07:21 AM
  2. Subquery
    By tomclavil in forum Queries
    Replies: 3
    Last Post: 02-27-2012, 03:05 AM
  3. TOP subquery
    By helpaccess in forum Queries
    Replies: 5
    Last Post: 08-30-2011, 10:28 AM
  4. Subquery sum?
    By anemoskkk in forum Access
    Replies: 0
    Last Post: 04-29-2011, 12:36 PM
  5. Subquery
    By combine21 in forum Queries
    Replies: 2
    Last Post: 09-17-2010, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums