Add Totals row to crostab query in Access

Posted on 2010-11-15
Last Modified: 2012-05-10

I use the following pivot query in Access database to track the daily work rate of 9 teams:

TRANSFORM Count(Master.Actual) AS Units
SELECT Master.Actual
FROM Teams INNER JOIN Master ON Teams.ID = Master.Teams_ID
WHERE (((Master.Actual) Is Not Null))
GROUP BY Master.Actual

This gives me the dataset in the attached picture..
 Datatable produced by the Query
I have spent way too long trying to add a row to the bottom of that dataset to show the totals..can anyone help / explain? (i am aware there is a totals button in Access 2007 but this is not the solution)

Thanks for reading
Question by:laserhalen
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Expert Comment

ID: 34141472
The totals button is the one that should give you column totals. Are you not using 2007, or is it not giving the desired value?

For row totals, add the value field again as a row heading.

Author Comment

ID: 34141668
Yes im using 2007 and it is the column totals im looking for but the query is to support a chart, so i need to calculate the totals as part of the query..dont i?

Row totals i can get, what im looking for is to add a single row to the bottom that shows the total for each this posible? i can generate this row on its own usint a different query (that searches the query i have posted) but then i couldn't add it to the table using UNION..

Thanks for the reply..
LVL 11

Accepted Solution

LambertHeenan earned 500 total points
ID: 34145861
You can use a union query.  

You have your crosstab query (Query-SC1) which returns the details, so then you just build a totals query that uses Query-SC1 as its source, summing all the relevant fields (1 through 9) with the first field in the quey being a simple 'calulated' field:  select distinct "Totals" as Total, sum(Query-SC1.1) as 1, etc. etc. and save that as SC1-Totals.

Then you can construct the union query

Select * from SC1 orderby Actual
Union select * from SC1-Totals

One very important caveat. Your cross tab must always return the same number of columns in order for the SC1-Totals and the Union query to work, so be sure to specify the column headings in the crosstab pivot clause...

TRANSFORM Count(Master.Actual) AS Units
SELECT Master.Actual
FROM Teams INNER JOIN Master ON Teams.ID = Master.Teams_ID
WHERE (((Master.Actual) Is Not Null))
GROUP BY Master.Actual
PIVOT Teams.ID In(1,2,3,4,5,6,7,8,9)

Office 365 Advanced Training for Admins

Special Offer:  Buy 1 course, get 2nd free!  Buy the 'Managing Office 365 Identities & Requirements' course w/ Accelerated TestPrep, and automatically receive the 'Enabling Office 365 Services' course FREE!

LVL 11

Assisted Solution

LambertHeenan earned 500 total points
ID: 34145880
P.S. Because the dates in column 1 have digits in them they will sort in front of the 'Totals' colum and so you get your result: totals at the bottom.

Author Comment

ID: 34168829
@ LambertHeenan
Excellent, thank you.
Took me a while to understand it..I thought you couldnt use DISTINCT in access?!

Anyway thanks again.
LVL 11

Expert Comment

ID: 34172732
Glad to help. And SQL without DISTINCT would be kind of restricted, don't you think? :-)

Featured Post

To Patch or not to Patch? That is the question!

Don't get caught out like thousands of others around the world in the recent Ransomware Fiasco!
- Why it's not a good idea to wait before Patching
- Sensible approaches to Patching discussed
- Add your feedback, comments and suggestions

Question has a verified solution.

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

Suggested Solutions

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 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