?
Solved

How can I create an accumulation column in query within the group?

Posted on 2005-05-05
17
Medium Priority
?
315 Views
Last Modified: 2012-06-21
I'm having a problem creating an accumulation column in query.  
This is sample table:
Line      Name      Date      Qty
1          xxx         1/2/05     1
2          xxx         2/5/05     3
3          xxx         3/4/05     2
4          yyy         1/3/05     2
5          yyy         1/5/05     3
6          yyy         2/6/05     4
7          yyy         3/4/05     1
8          zzz         1/6/05     2
9          zzz         3/5/05     1
10        zzz         5/8/05     1

This is the result with accum column that I'm looking for:
Line      Name      Date      Qty    Accum
1          xxx         1/2/05     1        1
2          xxx         2/5/05     3        4
3          xxx         3/4/05     2        6
4          yyy         1/3/05     2        2
5          yyy         1/5/05     3        5
6          yyy         2/6/05     4        9
7          yyy         3/4/05     1       10
8          zzz         1/6/05     2        2
9          zzz         3/5/05     1        3
10        zzz         5/8/05     1        4

"Line" column is primary key which sorts "Name" and "Date" in order.  I tried using DSum() function but there's no NameID assigned so it didn't work.  I have Access2000.  Please help....  
0
Comment
Question by:cherryjari
  • 6
  • 6
  • 3
  • +1
17 Comments
 
LVL 12

Expert Comment

by:Bob Lamberson
ID: 13941978
What are you grouping by? What exactly do you mean by an "accumulation column"?

Bob
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 13942884

Try this:
1. Move the "name" column to column 1 so that you are grouping first by name.
2. the Qty column remains as is with a "group by" in the total row of the grid.  
3. For the accum column : Accum Qty: Qty...........................but use"sum" instead of "group by"  in the total row of the query grid.
0
 
LVL 17

Expert Comment

by:Arji
ID: 13943189
You would do this in your report design.  Your Accum textbox should have the same control source as the Qty textbox. Then in the Accum textbox properties set the Running Total to 'Over Group'.  That will give you an accumulative(running) for Qty.
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
LVL 12

Expert Comment

by:Bob Lamberson
ID: 13944914
I don't see how the numbers in the accumulate column are the product of any consistant grouping in the tables. Can you explain?
0
 
LVL 1

Author Comment

by:cherryjari
ID: 13944979
I tried using Accum: DSum("[qty]","test","[line]<=" & [line]) in query and this is what I got ...

Line      Name      Date      Qty    Accum
1          xxx         1/2/05     1        1
2          xxx         2/5/05     3        4
3          xxx         3/4/05     2        6
4          yyy         1/3/05     2        8
5          yyy         1/5/05     3       11
6          yyy         2/6/05     4       15
7          yyy         3/4/05     1       16
8          zzz         1/6/05     2       18
9          zzz         3/5/05     1       19
10        zzz         5/8/05     1        20

I like the accum calculation to stop after each group of name.
0
 
LVL 1

Author Comment

by:cherryjari
ID: 13945018
Bob, the accum occurs in each group (xxx, yyy, zzz) within the same query.

Line      Name      Date      Qty    Accum
1          xxx         1/2/05     1        1
2          xxx         2/5/05     3        4
3          xxx         3/4/05     2        6

4          yyy         1/3/05     2        2
5          yyy         1/5/05     3        5
6          yyy         2/6/05     4        9
7          yyy         3/4/05     1       10

8          zzz         1/6/05     2        2
9          zzz         3/5/05     1        3
10        zzz         5/8/05     1        4
0
 
LVL 17

Expert Comment

by:Arji
ID: 13945129
My post will do what you want you want.  Did you try it?
0
 
LVL 1

Author Comment

by:cherryjari
ID: 13945176
Arji, I need to run this in query because this query is linked to other query and tables which calculate other things.
0
 
LVL 17

Assisted Solution

by:Arji
Arji earned 150 total points
ID: 13945290
<because this query is linked to other query and tables which calculate other things>

That shouldn't make any difference. You can use the "other query and tables" as the part of the recordsurce for the report.  It's much easier to do running totals and grouping totals in a report.
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 13945480
Cherryjari,
You are almost there.  I believe all you need to do now is drag name down again to the query grid as a "group by", but  uncheck the "show box" so that it becomes a constraint.
0
 
LVL 1

Author Comment

by:cherryjari
ID: 13945627
I tried that but it didn't work, the DSum() still calculates everything.  
0
 
LVL 38

Accepted Solution

by:
puppydogbuddy earned 600 total points
ID: 13945958
If name is on the query grid as a "groupby" twice, once with the show box checked, and once with the show box unchecked and it didn't work, first try using "sum" (without any criteria in the criteria row) instead of the DSum with the where clause. If that doesn't work, then try this trick:
create another calc field (I will call it "X") .  X: [Name] & [Line]......then go back to the syntax with the DSum, and try it this way:
                              DSum("[qty]","test","[name] in (X))
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 13945983
oops! that should be:

                          DSum("[qty]","test","[name] in (X)")
0
 
LVL 1

Author Comment

by:cherryjari
ID: 13946409
I get this error
'Database can't find the name "X" you entered in the expression'
'Unknown'
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 13946795
You forgot to add X as a column in your query grid as follows:
                               X: [Name] & [Line]
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 13946900
cherryjari,
If that didn't work, please post the sql code behind the query.  From the sql code, I'll be able to replicate and fix your query for you.  
0
 
LVL 1

Author Comment

by:cherryjari
ID: 13946964
SELECT test.Name, test.DDate, test.Qty, [name] & [Line] AS X, DSum("[qty]","test","[name] in (X)") AS Total
FROM test;

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question