Results 1 to 11 of 11
  1. #1
    falahsalih is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    38

    Group By data from different tables based on two sums

    Hi Everybody,
    I have 2 tables and one query as below

    1- Order Table

    OrderID | CustomerID | CusromerName | OrdDate

    2- Payment Table

    OrderID | Amount | PYMDate

    3- Order Deatails Query

    OrderID | ProductName | Quantity | Price | Total ([Quantity]*[Price])



    I want to group the orders based on the sum of total and sum of payments. I made another query as following :

    OrderID (From order table) | Payment (from payments table) | Total (From Order Details Query )
    at the Total field I set the above fields to
    Group By, Sum and Sum respectively

    I got true payments sum, however, the sum total are always duplicated. Any Help please ?
    Note that the three objects (two tables and query ) have relation (type : one to many) by the order ID

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Post the actual SQL statement for analysis.
    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
    falahsalih is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    38
    Quote Originally Posted by June7 View Post
    Post the actual SQL statement for analysis.
    Actually, I have no good background in Sql. but i post the actual sql by the sql view
    Code:
    SELECT order.[Order ID], Sum(Querytotal.Total) AS SumOfTotal, Sum(payments.Payment) AS SumOfPayment
    FROM ([order] INNER JOIN Querytotal ON order.[Order ID] = Querytotal.[Order ID]) INNER JOIN payments ON order.[Order ID] = payments.[Order ID]
    GROUP BY order.[Order ID];

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    What you said about wanting to "group the orders based on the sum of total and sum of payments" doesn't really make sense and the query you posted doesn't do that, it groups on the Order ID which is reasonable.

    What you need to do is an aggregate query that sums the Order details and an aggregate query that sums the payments. Then join those two aggregate queries with an outer join: "Include all records from OrderDetailsSum and only those records from PaymentSum that match"
    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
    falahsalih is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    38
    Quote Originally Posted by June7 View Post
    What you said about wanting to "group the orders based on the sum of total and sum of payments" doesn't really make sense and the query you posted doesn't do that, it groups on the Order ID which is reasonable.

    What you need to do is an aggregate query that sums the Order details and an aggregate query that sums the payments. Then join those two aggregate queries with an outer join: "Include all records from OrderDetailsSum and only those records from PaymentSum that match"
    Sorry, I have posted wrong Sql.
    I have update it

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The SQL is not changed and my suggestion still applies.
    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
    falahsalih is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    38
    Quote Originally Posted by June7 View Post
    The SQL is not changed and my suggestion still applies.
    Well, I followed your suggestion and made 2 queries. One for sum the payments, and the other one for sum the totals. Now, can you guide me how to join the 2 queries please. Many thanks

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Use the query builder. Treat the queries like tables.
    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
    falahsalih is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    38
    Quote Originally Posted by June7 View Post
    Use the query builder. Treat the queries like tables.
    It very confused to join them, I did what you suggest for me, then i got some conflict date. payment of one of the costumer has transformed to another customer

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The two queries should each have the OrderID field and link them on that field.

    Post sql statements for analysis.
    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.

  11. #11
    falahsalih is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    May 2014
    Posts
    38
    Quote Originally Posted by June7 View Post
    The two queries should each have the OrderID field and link them on that field.

    Post sql statements for analysis.
    Done.....Thank you very much

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

Similar Threads

  1. Replies: 4
    Last Post: 07-24-2013, 02:34 PM
  2. Replies: 0
    Last Post: 02-25-2013, 04:43 PM
  3. Query that sums data
    By accessnovice78 in forum Queries
    Replies: 3
    Last Post: 04-12-2012, 11:46 AM
  4. Query that sums a field based on 2 tables
    By scratchmb in forum Queries
    Replies: 6
    Last Post: 03-18-2012, 08:31 PM
  5. query that sums the data for me
    By citygov in forum Queries
    Replies: 4
    Last Post: 08-18-2011, 03:25 PM

Tags for this Thread

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