Results 1 to 9 of 9
  1. #1
    bd528 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    5

    Summing 2 subqueries

    Hi,



    I'm using the SQL below in Access 2010 :-

    Code:
    SELECT tblMerged.Col4, 
    (select sum(dupe.col8)
    from tblMerged as dupe
    where dupe.col4 = tblMerged.col4
    and dupe.col2 = "I"
    group by col8) AS Expr1, 
    (select sum(dupe.col12)
    from tblMerged as dupe
    where dupe.col4 = tblMerged.col4
    and dupe.col2 = "U"
    group by col12) AS Expr2, 
    (select sum([dupe.col11])
    from tblMerged as dupe
    where dupe.col4 = tblMerged.col4
    and dupe.col2 = "C") AS Expr3
    FROM tblMerged
    GROUP BY tblMerged.Col4;
    What do I need to add to this so that I can have another column showing Expr2 & Expr3 added together?

    Many thanks.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Did you try: Expr2 + Expr3 As Total

    If that doesn't work, I suspect the query has to repeat the subqueries in the expression.

    Suggest you build a report with this query as RecordSource and do the sum in textbox ControlSource: =Expr2 + Expr3
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    bd528 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    5
    That's a great idea, as I was planning to pull the results through to a report anyway.

    What would be the best approach of only showing results where Expr2 + Expr3 = Expr1?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You want to filter records? Criteria under the constructed Total column: Expr1

    Or include all records but just don't show value if the expression is not true?
    IIf(Expr2 + Expr3 = Expr1, Expr2 + Expr3, Null)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    bd528 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    5
    Thanks again, sorry I'm pretty new to queries. What would the criteria expression be for Expr1 in the query?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    No, Expr1 IS the criteria, under the Total column.

    Oh, wait Expr1 is also result of calc (doh!) - in this case, again a subquery. Again, a field created by expression cannot be referenced in WHERE clause. Must repeat the expression. This is getting messy.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    bd528 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    5
    Is there is another option rather than using subqueries?

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Don't know because don't really know your data structure nor what you are trying to accomplish for output. Maybe you are trying to do too much in query and should be building a report that uses Grouping & Sorting with aggregate calcs in header/footer.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    bd528 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2012
    Posts
    5
    You may be right. I think, in the short term, I'll create another query based on the original and do filtering via that.
    But thank you for your help!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Structuring Subqueries
    By dandoescode in forum Queries
    Replies: 1
    Last Post: 03-13-2012, 06:42 PM
  2. Creating subqueries in SQL view
    By AmyM in forum Queries
    Replies: 2
    Last Post: 11-20-2011, 05:21 PM
  3. LEFT/RIGHT/OUTER JOINS in a set of subqueries
    By luckycharms in forum Queries
    Replies: 1
    Last Post: 08-01-2011, 05:06 PM
  4. summing
    By nashr1928 in forum Forms
    Replies: 18
    Last Post: 04-05-2011, 05:01 PM
  5. need help with summing a range
    By sundance0000 in forum Access
    Replies: 3
    Last Post: 01-12-2010, 02:12 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