Group by prefix characters, then sum another column

  • Thread starter Thread starter Williams
  • Start date Start date
W

Williams

Does any one know how to use the Totals feature to group by the first
character in a field, then sum the values in another column that share a
common prefix? I need to do this in a query.

I have one column that has codes such as P001, P002, P003, U001, U002, U003.
I want to group all of the 'P' records together, then sum another column. I
want to do the same for the 'U' records.

help is appreciated.
 
Williams said:
Does any one know how to use the Totals feature to group by the first
character in a field, then sum the values in another column that share a
common prefix? I need to do this in a query.

I have one column that has codes such as P001, P002, P003, U001, U002, U003.
I want to group all of the 'P' records together, then sum another column. I
want to do the same for the 'U' records.


You need to use a calculated field to get the first
character, then you can group on it:

SELECT Left([a field], 1) As firstchar,
Sum([another column]) As Total
FROM [some table]
GROUP BY Left([a field], 1)
 
You would use
Left(SomeField,1)
as a calculated field and group on the calculation. You could not include the
field in the query or that would defeat the grouping on just the first character.

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

sum two columns 5
Simple Question on Addition 3
Validation of cell to enter data 39
SUM in a UNION query 2
SUM NOT INCLUDING ALL RECORDS 4
Access Query problem 1
SUBQUERY BLUES 8
trying to get a sum in access 2003 1

Back
Top