Another Question about Select Queries

K

ken

Using the Query below, is it possible to also have a row at the bottom that
will
give me the totals for each column.

Thanks
Ken

SELECT
Format([opendate],' mmmm\,yyyy') AS [Month],
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls
GROUP BY Format([opendate],' mmmm\,yyyy');
 
G

Gerald Stanley

Try this UNION query

SELECT
Format([opendate],' mmmm\,yyyy') AS [Month],
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls
GROUP BY Format([opendate],' mmmm\,yyyy')
UNION
"Total",
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls

This is 100% untested air-code.

Hope This Helps
Gerald Stanley MCSD
 
L

Lynn Trapp

Gerald,
That appears to work (I only tested it slightly), except that you left the
word "Select" out of the second query. Modify it to this and it should work:

UNION
SELECT "Total",
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls


--
Lynn Trapp
MS Access MVP
(e-mail address removed)
www.ltcomputerdesigns.com
Access Security: www.ltcomputerdesigns.com/Security.htm


Gerald Stanley said:
Try this UNION query

SELECT
Format([opendate],' mmmm\,yyyy') AS [Month],
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls
GROUP BY Format([opendate],' mmmm\,yyyy')
UNION
"Total",
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls

This is 100% untested air-code.

Hope This Helps
Gerald Stanley MCSD
-----Original Message-----
Using the Query below, is it possible to also have a row at the bottom that
will
give me the totals for each column.

Thanks
Ken

SELECT
Format([opendate],' mmmm\,yyyy') AS [Month],
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls
GROUP BY Format([opendate],' mmmm\,yyyy');


.
 
G

Gerald Stanley

Lynn

Thanks for spotting that. A result of poor cut and paste.

Gerald
-----Original Message-----
Gerald,
That appears to work (I only tested it slightly), except that you left the
word "Select" out of the second query. Modify it to this and it should work:

UNION
SELECT "Total",
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls


--
Lynn Trapp
MS Access MVP
(e-mail address removed)
www.ltcomputerdesigns.com
Access Security: www.ltcomputerdesigns.com/Security.htm


Gerald Stanley said:
Try this UNION query

SELECT
Format([opendate],' mmmm\,yyyy') AS [Month],
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls
GROUP BY Format([opendate],' mmmm\,yyyy')
UNION
"Total",
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls

This is 100% untested air-code.

Hope This Helps
Gerald Stanley MCSD
-----Original Message-----
Using the Query below, is it possible to also have a row at the bottom that
will
give me the totals for each column.

Thanks
Ken

SELECT
Format([opendate],' mmmm\,yyyy') AS [Month],
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls
GROUP BY Format([opendate],' mmmm\,yyyy');


.


.
 
L

Lynn Trapp

No problem, Gerald. We've all been there, done that.

--
Lynn Trapp
MS Access MVP
(e-mail address removed)
www.ltcomputerdesigns.com
Access Security: www.ltcomputerdesigns.com/Security.htm


Gerald Stanley said:
Lynn

Thanks for spotting that. A result of poor cut and paste.

Gerald
-----Original Message-----
Gerald,
That appears to work (I only tested it slightly), except that you left the
word "Select" out of the second query. Modify it to this and it should work:

UNION
SELECT "Total",
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls


--
Lynn Trapp
MS Access MVP
(e-mail address removed)
www.ltcomputerdesigns.com
Access Security: www.ltcomputerdesigns.com/Security.htm


Gerald Stanley said:
Try this UNION query

SELECT
Format([opendate],' mmmm\,yyyy') AS [Month],
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls
GROUP BY Format([opendate],' mmmm\,yyyy')
UNION
"Total",
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls

This is 100% untested air-code.

Hope This Helps
Gerald Stanley MCSD

-----Original Message-----
Using the Query below, is it possible to also have a row
at the bottom that
will
give me the totals for each column.

Thanks
Ken

SELECT
Format([opendate],' mmmm\,yyyy') AS [Month],
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls
GROUP BY Format([opendate],' mmmm\,yyyy');


.


.
 
T

Tom Ellison

Dear Ken:

It is possible, and it is fairly easy. However, there are better ways
to get this through forms and reports.

Because you want the totals row at the bottom, you must have some way
of sorting to get this. I would add a column [BT] (Bottom Total)
which would be 0 for the current rows of your query, and 1 for the
bottom total. Then create a UNION ALL with this query and add another
SELECT query to give the totals in which BT is 1. Add an ORDER BY the
puts everything in sequence, with the BT column first.

It might look like this:

SELECT 0 AS BT, Format([opendate],' mmmm\,yyyy') AS [Month],
Count(calls.Severity1) AS CountOfSeverity1,
Count(calls.Severity2) AS CountOfSeverity2,
Sum(IIf(calls.Calltyp="Question",1,0)) AS Question,
Sum(IIf(calls.Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls
GROUP BY Format([opendate],' mmmm\,yyyy')
UNION ALL
SELECT 1 AS BT, NULL, COUNT(Severity1), COUNT(Severity2),
Sum(IIf(Calltyp="Question",1,0)) AS Question,
Sum(IIf(Calltyp="Other",1,0)) AS Other,
Count(Format([opendate],' mmmm')) AS total
FROM calls
ORDER BY BT, Format([opendate],' mmmm\,yyyy');

Again, putting such detail into a query is rare and certainly
unnecessary if the data is to be displayed in a form or report. There
are much better ways to do this. However, in the spirit of ROLLUPs
and such, these kind of approaches are more and more common in
queries. In fact, we are tending here toward queries that look more
and more like the reports they feed, but only for some very complex
reasons (such as overcoming the slow speed of reports with multiple
subreports, for example.) For anything less than expert level
reasons, I'd prefer to discourage you from using this kind of feature,
but if you really need it, there it is.

Tom Ellison
Microsoft Access MVP
Ellison Enterprises - Your One Stop IT Experts
 

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

Last Question on Select Query 3
Union query error 3
Totals query rework 1
Select Query 4
Query on Duration 1
Cross-Tab Query 2
Crosstab : PIVOT ... IN (...) problem ! 6
Union Query 3

Top