Access 2007 Need to count ocourances of "Running" and incrementally number them.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • union51
    New Member
    • May 2010
    • 5

    #1

    Access 2007 Need to count ocourances of "Running" and incrementally number them.

    In Access 2007, I have a query it has 2 collumns one is "Location" and the other is titled "Activity". Lets say "Location" has 2 choices "Here" or "There". While "Activity" has several that can be done at both locations. Lets say "Eat, Drink, Rest, Run, Walk" I want to count on the rows of data incrementally how many times I run, here and add incrementally in a third collumn how many times it is done. I can have this in a new query or the existing one.

    So It would look like (table example):
    Run Here
    Run There
    Walk Here
    Run There
    Run Here

    And the results would be
    Run Here 1
    Run There 1
    Walk Here 1
    Run There 2
    Run Here 2
    Last edited by union51; May 21 '10, 04:21 PM. Reason: referenced different item than I asked in the question
  • Jim Doherty
    Recognized Expert Contributor
    • Aug 2007
    • 897

    #2
    Originally posted by union51
    In Access 2007, I have a query it has 2 collumns one is "Location" and the other is titled "Activity". Lets say "Location" has 2 choices "Here" or "There". While "Activity" has several that can be done at both locations. Lets say "Eat, Drink, Rest, Run, Walk" I want to count on the rows of data incrementally how many times I run, here and add incrementally in a third collumn how many times it is done. I can have this in a new query or the existing one.

    So It would look like (table example):
    Run Here
    Run There
    Walk Here
    Run There
    Run Here

    And the results would be
    Run Here 1
    Run There 1
    Walk Here 1
    Run There 2
    Run Here 2
    Welcome to Bytes :)

    Assuming to have read you right this relies on a table called Table1 having two columns Location and Activity containing the data you describe. Paste this into the SQL window of an Access query and run it. It presents three columns one being concatanated (which you can delete if you wish. Remember you must have a table called table1 or alter the references in this SQL replacing Table1 with your own

    Code:
    SELECT Table1.Location, Table1.Activity, [Location] & " " & [Activity] AS [Physical Activity], Count(Table1.Activity) AS Result
    FROM Table1
    GROUP BY Table1.Location, Table1.Activity, [Location] & " " & [Activity];

    Comment

    • union51
      New Member
      • May 2010
      • 5

      #3
      Originally posted by Jim Doherty
      Welcome to Bytes :)

      Assuming to have read you right this relies on a table called Table1 having two columns Location and Activity containing the data you describe. Paste this into the SQL window of an Access query and run it. It presents three columns one being concatanated (which you can delete if you wish. Remember you must have a table called table1 or alter the references in this SQL replacing Table1 with your own

      Code:
      SELECT Table1.Location, Table1.Activity, [Location] & " " & [Activity] AS [Physical Activity], Count(Table1.Activity) AS Result
      FROM Table1
      GROUP BY Table1.Location, Table1.Activity, [Location] & " " & [Activity];
      Thank you for the quick reply....
      Is there a way to count each row incrementally?
      So that I could have
      here run 1
      here run 2
      there run 1
      there walk 1
      here run 3
      Last edited by union51; May 21 '10, 05:01 PM. Reason: added thankyou

      Comment

      • Jim Doherty
        Recognized Expert Contributor
        • Aug 2007
        • 897

        #4
        Originally posted by union51
        Thank you for the quick reply....
        Is there a way to count each row incrementally?
        So that I could have
        here run 1
        here run 2
        there run 1
        there walk 1
        here run 3
        oh I get you now a running sum....well just now I am going for a meal so unless someone else contributes this in the meantime.... I will come back to you in a bit

        Comment

        • union51
          New Member
          • May 2010
          • 5

          #5
          Originally posted by Jim Doherty
          oh I get you now a running sum....well just now I am going for a meal so unless someone else contributes this in the meantime.... I will come back to you in a bit
          No problem, I had figured I could do it in a VBA loop then had no idea on how to count my instances.
          Thank you for your help.

          Comment

          • Jim Doherty
            Recognized Expert Contributor
            • Aug 2007
            • 897

            #6
            Originally posted by union51
            No problem, I had figured I could do it in a VBA loop then had no idea on how to count my instances.
            Thank you for your help.
            Hi again

            You can play with a loop if you wish to hone your VBA skills but SQL can handle this too.
            To make the running sum work add an extra field called 'id' to the table and set it to autonumber then let it fill the column with a sequence of numbers.

            Now that we have a series of some numeric data available we can now obviously implement a running sum based on counting the 'id' column and significantly relate that to the location column and the activity column.

            Given that we are dealing with the table called table1 as mentioned earlier. the following SQL statement will give you your running sum per location/activity as desired

            Code:
            SELECT LA.id, LA.Location, LA.Activity, LA.[Running Sum]
            FROM [SELECT LA.id, LA.Location, LA.Activity, (select count([id]) FROM table1 where id <=LA.id and location=LA.location and activity=LA.activity) AS [Running Sum]
            FROM Table1 AS LA]. AS LA
            ORDER BY LA.Location, LA.Activity, LA.[Running Sum];

            Comment

            • union51
              New Member
              • May 2010
              • 5

              #7
              Sorry, I was out mowing my yard by popular demand.
              I am getting a error on the bracket between from & select. Will try to figure it out however.

              Comment

              • Jim Doherty
                Recognized Expert Contributor
                • Aug 2007
                • 897

                #8
                Originally posted by union51
                Sorry, I was out mowing my yard by popular demand.
                I am getting a error on the bracket between from & select. Will try to figure it out however.
                I don't know what happened there... fixed

                Code:
                SELECT LA.id, LA.Location, LA.Activity, LA.[Running Sum] 
                FROM (SELECT LA.id, LA.Location, LA.Activity, (select count([id]) FROM table1 where id <=LA.id and location=LA.location and activity=LA.activity) AS [Running Sum] 
                FROM Table1 AS LA) AS LA 
                ORDER BY LA.Location, LA.Activity, LA.[Running Sum];

                Comment

                • union51
                  New Member
                  • May 2010
                  • 5

                  #9
                  Originally posted by Jim Doherty
                  I don't know what happened there... fixed

                  Code:
                  SELECT LA.id, LA.Location, LA.Activity, LA.[Running Sum] 
                  FROM (SELECT LA.id, LA.Location, LA.Activity, (select count([id]) FROM table1 where id <=LA.id and location=LA.location and activity=LA.activity) AS [Running Sum] 
                  FROM Table1 AS LA) AS LA 
                  ORDER BY LA.Location, LA.Activity, LA.[Running Sum];
                  Oh, well that is perfect thank you so much for the help!

                  Comment

                  Working...