tags:

views:

505

answers:

1

I have a query stored in MS Access which is doing a standard select from an Access table. I would like to add a summary row at the end showing sums for some of the data above.

I have looked at DSum() but it isn't suitable as I would have to include the running total on each row as opposed to just the end.

Also, note that I don't want to sum data in column a - I would like to get an empty field for the summary of column a.

Example:

a | b | c
-------------
0 | 1 | 2
1 | 1 | 9

  | 2 | 11 <-- Sums data above

Does anyone know how this problem can be solved in Access? An alternative might be to define a second query which does the aggregation and then merge it with the recordset of the first one, but this doesn't seem particularly elegant to me.

In SQL server it is apparently possible to use "COMPUTE" or "ROLLUP" but these are not supported under MS Access.

+3  A: 

You can use a union query:

SELECT "" As Sort, a,b,c FROM Table
UNION ALL
SELECT "Total" As Sort, Sum(a) As A, Sum(b) As b, Sum(c) As C FROM Table
ORDER BY Sort

EDIT:

SELECT "" As Sort, a,b,c FROM Table
UNION ALL
SELECT "Total" As Sort, "" As A, Sum(b) As b, Sum(c) As C FROM Table
ORDER BY Sort
Remou
I have made a small change, instead of 1 as Sort, I have used "" and "Total", I think this is better.
Remou
To add a bit of difficulty to this, I don't want to sum up all the columns just b and c for example.
Patrick
You can use dummy values. Please see my edit.
Remou
Excellent, thanks :) Works a charme.
Patrick
-1 "David W. Fenton" is correct: this is something for a report and not suited to a table. If the Access UI didn't obscure VIEWs the error would be more obvious.
onedaywhen
@onedaywhen so how do you build HTML using a report, as per @Patrick's comment? Furthermore Access is often used as a convenient means of processing data, reports do not translate well to other applications.
Remou
@onedaywhen By the way, the above is a query, not a table.
Remou
If I were outputting to HTML, I'd do it VBA code. Indeed, I do it a lot. Depending on how I processed the data, I might summarize the data while traversing the recordset and outputting each row (i.e., adding up totals, and so forth). When more convenient, I might open another recordset for the totals. That wouldn't be any less efficient than a UNION and avoids sorting issues. But the full context wasn't provided, so I think it's ridiculous that anyone should be whining that readers didn't guess the context.
David-W-Fenton
@David W. Fenton I think you may have posted this comment in the wrong place. It appears to be a reply to the original post. That aside, I do not believe that Patrick A is whining, it seems to be a civilly expressed request for clarification of your not quite so civil statement.
Remou
@Remou: "how do you build HTML" -- not using Access Database Engine SQL, that's for sure! Save the query (lower case) as a persisted database object and it becomes a Query (Access UI) a.k.a. (VIEW = viewed table). Sorry for mistaking you as one of those sloppy writers who use 'query' when they mean 'Query'... 'cos I just remembered you're the one who doesn't use Query objects or VIEWs, you prefer to persist SQL code in a table lol!
onedaywhen