Solved

Add Totals row to crostab query in Access

Posted on 2010-11-15
6
966 Views
Last Modified: 2012-05-10
Hi,

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
PIVOT Teams.ID

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
0
Comment
Question by:laserhalen
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:infolurk
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.
0
 

Author Comment

by:laserhalen
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 column..is 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..
0
 
LVL 11

Accepted Solution

by:
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)

0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 11

Assisted Solution

by:LambertHeenan
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.
0
 

Author Comment

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

Anyway thanks again.
0
 
LVL 11

Expert Comment

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

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access 2016 7 35
Calculation in Access 5 27
Create macro from runcode 30 26
Runtime Error 9 - Subscript out of Range (Check to see if Sheet Exists) 14 25
Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

832 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