Results 1 to 3 of 3
  1. #1
    lawrecp is offline Novice
    Windows 11 Office 365
    Join Date
    Feb 2025
    Posts
    1

    Using Subquery "summed totals" in a Primary Query. Not working as desired. Please help.


    I have a data set of line item payments that typically have 2-3 line items (partial payments) for one ClaimID. I am trying to create a query with a clean output of expected revenue per ClaimID. If the partial payments were added together then subtracted from "aquisition cost" then it would show a positive revenue. I created a subquery to consolidate the ClaimIDs and SUM the total payments but when I add this subquery to my Primary Query and drag the [SumTotalPayments] into the query, it doesnt consolidate the ClaimIDs, only adds the [SumTotalPayments] to each line item. Hoping there is an easy fix. Spent too many hours trying to figure it out. Any help would be greatly appreciated. Happy to make clarifications if my explanation doesn't make sense. Pictures with #1 are my Primary Query and #2 is by subquery and #3 is combined query. Thankyou, Phillip
    Attached Thumbnails Attached Thumbnails Primary Query 1.png   Primary Query SQL.png   Primary w Subquery.png   Primary w Subquery SQL.jpg   Subquery.png  


  2. #2
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,180
    First of all, the conditions CPT= "j9022" and servicedate between should probably be in the WHERE clause and not the having. This way the conditions are applied before the grouping and the query would be faster.
    If you need the revenue per claim, I would say you need to group by claim, with the sum of the payments, and the sum of the costs. You can use this query as a basis to calculate sum of payments - sum of costs

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Might consider building a report and use its Sorting & Grouping with aggregate calculations. This will allow showing detail records as well as summary calcs.

    Otherwise, query needs to aggregate both TotalActualA and Payments, as already noted by NoellaG.

    Producing your query 3 without the payments sum repeating can be done but gets really complicated and would likely slow query performance.
    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.

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

Similar Threads

  1. Replies: 5
    Last Post: 04-13-2020, 01:20 PM
  2. Using Summed vb Constants
    By dunc723 in forum Programming
    Replies: 3
    Last Post: 05-07-2018, 07:36 PM
  3. A subquery inside a subquery
    By tbjmobile@gmail.com in forum Queries
    Replies: 15
    Last Post: 04-22-2018, 11:48 AM
  4. Replies: 9
    Last Post: 02-05-2018, 09:45 AM
  5. Replies: 3
    Last Post: 04-07-2015, 10:37 AM

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