Solved

# Want a row that shows the total of the result set

Posted on 2008-10-23
134 Views
I have a query that shows a user, and the number of rows where status=3, and the number of rows where status = 999. At the bottom of the result set I want a row that shows the total for the two columns. How would I modify the query below so that it shows the total at the bottom of the result set?

Example:

User             NeedApproval       NotApproved
Casey      6              6
Jess      2              1
Stephen      3              2
Total              11                         9
``````Select analyst_brokerrep, sum(NeedApproval) as NeedApproval, sum(NotApproved) as NotApproved
From (
select analyst_brokerrep, Case When [status] = 3 Then count(status) END as NeedApproval,
Case When [status] = 999 Then count(status) END as NotApproved
FROM         tbl_Offer_1_Price INNER JOIN
EnrollCustomer ON tbl_Offer_1_Price.DealCapID = EnrollCustomer.DealCapID
where (status IN (3, 999)) AND (analyst_brokerrep is not null)
Group By analyst_brokerrep, [status]
) as Z
Group By analyst_brokerrep
``````
0
Question by:utlonghornjulie
• 2
• 2

LVL 39

Expert Comment

ID: 22788165
Here you go.
``````;with SumInfo as
(Select analyst_brokerrep, sum(NeedApproval) as NeedApproval, sum(NotApproved) as NotApproved
From (
select analyst_brokerrep, Case When [status] = 3 Then count(status) END as NeedApproval,
Case When [status] = 999 Then count(status) END as NotApproved
FROM         tbl_Offer_1_Price INNER JOIN
EnrollCustomer ON tbl_Offer_1_Price.DealCapID = EnrollCustomer.DealCapID
where (status IN (3, 999)) AND (analyst_brokerrep is not null)
Group By analyst_brokerrep, [status]
) as Z
Group By analyst_brokerrep)

select * as Sort from SumInfo
union all
select 'Total',sum(NeedApproval),(NotApproved)
from SumInfo
``````
0

LVL 3

Author Comment

ID: 22788261
I get the following error when I try and run the query:

Incorrect syntax near the keyword 'as'.
0

LVL 39

Accepted Solution

BrandonGalderisi earned 500 total points
ID: 22788370
oops...

started to do something, realized didn't need it, forgot to rollback the whole set of changes.
``````;with SumInfo as
(Select analyst_brokerrep, sum(NeedApproval) as NeedApproval, sum(NotApproved) as NotApproved
From (
select analyst_brokerrep, Case When [status] = 3 Then count(status) END as NeedApproval,
Case When [status] = 999 Then count(status) END as NotApproved
FROM         tbl_Offer_1_Price INNER JOIN
EnrollCustomer ON tbl_Offer_1_Price.DealCapID = EnrollCustomer.DealCapID
where (status IN (3, 999)) AND (analyst_brokerrep is not null)
Group By analyst_brokerrep, [status]
) as Z
Group By analyst_brokerrep)

select * from SumInfo
union all
select 'Total',sum(NeedApproval),(NotApproved)
from SumInfo
``````
0

LVL 3

Author Comment

ID: 22788418
Thanks it worked!
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

SQL Select * from 6 39
SQL Date from a string 4 64
PERFORMANCE OF SQL QUERY 13 72
SQL Trigger or Function that updates table with old values 5 42
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…