Create a Query that provides a running total but grouped?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gandalf186
    New Member
    • Sep 2006
    • 8

    Create a Query that provides a running total but grouped?

    I need to create a query that produces running totals for every group within my table for example i wish to see: -

    Group A

    [running total] 1
    [running total] 5
    [running total] 9
    [running total] 15

    Group B

    [running total] 1
    [running total] 2
    [running total] 20

    etc..

    I have worked out how to produce a running total but this will produce it for all records in the table and not split it (or reset it when it find a new group). Also this method works on the Unique ID which will fail due to the grouping required.
  • PEB
    Recognized Expert Top Contributor
    • Aug 2006
    • 1418

    #2
    Hi,

    Try to do a query that do the sum by your wanted groups and make Union query with your not aggregate query

    :)

    Comment

    • Gandalf186
      New Member
      • Sep 2006
      • 8

      #3
      Thanks for that, I think I understand what you mean!!

      But if I sum by group and then run a query on the results, won't that just give me the running total of the groups?

      I am looking to list all records by group with all the fields in each group showing a running total. After I have achieved that I only want to see records in ech group that reach a certain value.


      Thanks again

      Comment

      • PEB
        Recognized Expert Top Contributor
        • Aug 2006
        • 1418

        #4
        You mention that u've done the running total! How did you do it?

        Maybe we can integrate sth with this?

        :)

        Comment

        • Gandalf186
          New Member
          • Sep 2006
          • 8

          #5
          I used a sql query to create an alias and then compared ID with ID : -

          SELECT tbl_Alias.group s, tbl_Alias.id AS Expr1, (SELECT Sum([table3].[totals]) AS Total
          FROM [table3]
          WHERE ((([table3].[ID])<=[tbl_alias].[ID]));) AS [Running Total], tbl_Alias.total s
          FROM table3 AS tbl_Alias, Table3
          GROUP BY tbl_Alias.group s, tbl_Alias.id, tbl_Alias.total s;

          This wirks by creating a running total based on my unique ID field however I need to do this on the records within each group.

          Any Ideas??

          Comment

          • PEB
            Recognized Expert Top Contributor
            • Aug 2006
            • 1418

            #6
            So it seems to be reachable using VB functions and a temporary table to store the results! This is the way that I see!

            Do u want to proceed in this direction?

            Comment

            • Gandalf186
              New Member
              • Sep 2006
              • 8

              #7
              I will be using the results as a transfer to MS Excel (the only reason I am using Access is because of the amount of rows required (over 65000 records). I will be happy to use any method avaliable.

              Comment

              • PEB
                Recognized Expert Top Contributor
                • Aug 2006
                • 1418

                #8
                So this have to be pasted into a module in Access:

                Code:
                Global last_criteria
                Global last_used
                
                Function Set_last(Values, criterias)
                last_criteria = nts(criterias)
                last_used = nts(Values)
                Set_last = last_used
                End Function
                
                Function show_last(Ref)
                'Stop
                show_last = last_used
                End Function
                Function show_last_criteria(Ref)
                show_last_criteria = last_criteria
                End Function
                For running sum for a Group your column should be:
                Run_sum: IIF(show_last_c riteria([Group_Name])=[Group_Name],Set_last(val(s how_last([Group_Name]))+ [myvalues],[Group_Name]),Set_last([myvalues], [Group_Name]))


                So you need to change in this expression:
                [Group_Name] with the name of the field that contains your groups
                And
                [myvalues] with the Field on which you are running sum

                From query choose make table query and give the name of the table in which will be stored the result!

                Than choose Run and open the respective table!

                :)

                Comment

                • Gandalf186
                  New Member
                  • Sep 2006
                  • 8

                  #9
                  Thanks for the script, however I am getting a sub or function not defined.. with the nts highlighted??

                  Comment

                  • Gandalf186
                    New Member
                    • Sep 2006
                    • 8

                    #10
                    I have removed the nts references so now it looks like: -

                    Function Set_last(Values , criterias)
                    last_criteria = criterias
                    last_used = Values
                    Set_last = last_used
                    End Function

                    the code runs okay but my resulting table is duplicating the running sum?

                    i.e.

                    id totals Run_sum groups
                    1 0.2 0.4 group1 (added 0.4 instead of 0.2)
                    2 0.2 0.8 group1 (added 0.4 instead of 0.2)
                    3 0.5 1.8 group1 (added 1.0 instead of 0.5)
                    4 0.7 3.2 group1 etc...
                    5 0.8 4.8 group1
                    6 0.2 5.2 group1
                    7 0.5 6.2 group1
                    8 0.1 0.2 group2
                    9 0.1 0.4 group2
                    10 0.1 0.6 group2
                    11 0.1 0.8 group2
                    12 0.1 1 group2
                    13 0.1 1.2 group2

                    Comment

                    • PEB
                      Recognized Expert Top Contributor
                      • Aug 2006
                      • 1418

                      #11
                      Ok!

                      This is because you use grouping, remove your grouping!

                      And any conditions under this fields!

                      Comment

                      • Gandalf186
                        New Member
                        • Sep 2006
                        • 8

                        #12
                        Excellent!!

                        Great stuff, now I have all I need to start my Project!

                        Thank you very much.

                        Comment

                        • PEB
                          Recognized Expert Top Contributor
                          • Aug 2006
                          • 1418

                          #13
                          Great man!!!
                          :)

                          Have a nice day!

                          :)

                          Comment

                          • Narender Sagar
                            New Member
                            • Jul 2011
                            • 189

                            #14
                            Hi, I was trying to use this code in my query, but I'm also getting duplicate running sum. I'm not able to understand, which groupings I need to remove! Can you please help to fix this.
                            Thanks

                            Comment

                            • Narender Sagar
                              New Member
                              • Jul 2011
                              • 189

                              #15
                              Oh! I removed one calculated column from the query and result was perfect. Thanks, matter resolved.

                              Comment

                              Working...