Results 1 to 6 of 6
  1. #1
    ribena1980 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2015
    Posts
    7

    Subtracting One Sum Total From Another

    Hi,



    The following are fields in a table I have

    TransactionMonth
    Section
    Amount

    What I am looking to do is create a query or report that will subtract the total for Section A from Section B and group it by month.

    It sounds straightforward enough but there is something that I am missing.

    Has anyone got any ideas?

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    is there is something that I am missing.
    well, section is a reserved word so may be causing a problem, but what is missing is the sql that you are using at the moment in your query and some sample data and the result you want, otherwise we are just guessing

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2013
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    There is probably a more elegant way to do it with sub queries but I think this works:

    Crete aa query called qryTotalA with the following SQL:
    Code:
    TRANSFORM Sum(YourTable.Amount) AS SumOfAmountSELECT YourTable.TransactionMonth
    FROM YourTable
    WHERE (((YourTable.Section)="a"))
    GROUP BY YourTable.TransactionMonth
    PIVOT YourTable.Section;
    Crete aa query called qryTotalB with the following SQL:
    Code:
    TRANSFORM Sum(YourTable.Amount) AS SumOfAmountSELECT YourTable.TransactionMonth
    FROM YourTable
    WHERE (((YourTable.Section)="b"))
    GROUP BY YourTable.TransactionMonth
    PIVOT YourTable.Section;
    Create a query with the following SQL which will give the required results:
    Code:
    SELECT qryTotalA.TransactionMonth, qryTotalA.a, qryTotalB.b, [a]-[b] AS ResultFROM qryTotalB INNER JOIN qryTotalA ON qryTotalB.TransactionMonth = qryTotalA.TransactionMonth;
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2013
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Forgot to mention that you would need to substitute "YourTable" in the first and second query with the actual name of your table.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    ribena1980 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2015
    Posts
    7
    Hi Bob,

    Many Thanks for that. I think it seems to be doing what I want on the 2 sub queries.

    For the query to link them up, can you just explain the
    qryTotalA.a, qryTotalB.b, [a]-[b] bit

    as I understand that qryTotalA and qrtTotalB relate to the the name of the actual queries, but what do a and b relate to?

    Thanks,
    Mark

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2013
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by ribena1980 View Post
    Hi Bob,

    Many Thanks for that. I think it seems to be doing what I want on the 2 sub queries.

    For the query to link them up, can you just explain the
    qryTotalA.a, qryTotalB.b, [a]-[b] bit

    as I understand that qryTotalA and qrtTotalB relate to the the name of the actual queries, but what do a and b relate to?

    Thanks,
    Mark
    Hi Mark,
    It may help to look at the third query in design view.
    This query joins the other two using the month field. [a] and [b] are the fields from the other two queries which are shown in the third query and also used in the calculated field (called Result) [a]-[b].
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

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

Similar Threads

  1. Subtracting Hours
    By Mark256 in forum Queries
    Replies: 4
    Last Post: 10-08-2014, 10:56 PM
  2. Error subtracting
    By Derrick T. Davidson in forum Queries
    Replies: 4
    Last Post: 07-25-2013, 01:18 AM
  3. Replies: 2
    Last Post: 05-19-2011, 04:53 PM
  4. Subtracting Fields
    By zakslaher in forum Access
    Replies: 15
    Last Post: 03-30-2010, 09:36 AM
  5. Subtracting Records
    By e13851mlee in forum Programming
    Replies: 1
    Last Post: 12-17-2009, 04:50 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