Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 How to reverse the sum!

Author  Topic 

Josephptran
Starting Member

14 Posts

Posted - 2009-04-10 : 15:22:02
Hello

I have created a store procedure to display a report, I did what a report supposes to be. But there is one small thing that I dislike the way it displays, and that is:

The output displays like below:

SocialSecurityNumber ClearedDate Type DDS TOTALCLEARED
2
111223333 1/1/2009 DE BO 1
452127891 1/5/2009 SM CT 1

the number 2 is sum of two rows of the column "TOTALCLEARED". I wish it to display 2 at the bottom of column "TotalCleared" (not display 2 on top)

DO ANYONE HERE KNOW HOW?

HERE IS MY STORE PROCEDURE FOR THE DISPLAY ABOVE

ALTER PROCEDURE [dbo].[WklyCasesClearedWithNoReturns] 
-- Add the parameters for the stored procedure here
@Start Datetime,
@End Datetime,
@Office varchar(5)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

--select * from ROCAP


create table #temp
(
SocialSecurityNumber varchar(9),
ClearedDate datetime,
[Type] varchar(5),
DDS varchar(50),
TotalCleared int
)

if @office = 'ALL'
begin

--DE
insert into #temp
select SocialSecurityNumber, DEClearedDate as ClearedDate, 'DE' as [Type], DDS, NULL
from ROCAPData where
DEClearedDate between @start and @End
and DESecondClearedDate is NULL
and DEThirdClearedDate is NULL
and DEFourthClearedDate is NULL
Order BY ISNULL( DEClearedDate, '31-Dec-2090')

--Somatic
insert into #temp
select SocialSecurityNumber, SomaticMCClearedDate as ClearedDate, 'SM' as [Type], DDS, NULL
from ROCAPData where
SomaticMCClearedDate between @start and @End
and SomaticMCSecondClearedDate is NULL
and SomaticMCThirdClearedDate is NULL
and SomaticMCFourthClearedDate is NULL
Order BY ISNULL( SomaticMCClearedDate, '31-Dec-2090')
--Psyc
insert into #temp
select SocialSecurityNumber, PsycMCClearedDate as ClearedDate, 'PM' as [Type], DDS, NULL
from ROCAPData where
PsycMCClearedDate between @Start and @End
and PsycMCSecondClearedDate is NULL
and PsycMCThirdClearedDate is NULL
and PsycMCFourthClearedDate is NULL
Order BY ISNULL( PsycMCClearedDate, '31-Dec-2090')


end
else
begin
--DE
insert into #temp
select SocialSecurityNumber, DEClearedDate as ClearedDate, 'DE' as [Type], DDS, NULL
from ROCAPData where
DEClearedDate between @Start and @End
and DESecondClearedDate is NULL
and DEThirdClearedDate is NULL
and DEFourthClearedDate is NULL
and DDS = @office
Order BY ISNULL( DEClearedDate, '31-Dec-2090')
--Somatic
insert into #temp
select SocialSecurityNumber, SomaticMCClearedDate as ClearedDate, 'SM' as [Type], DDS, NULL
from ROCAPData where
SomaticMCClearedDate between @Start and @End
and SomaticMCSecondClearedDate is NULL
and SomaticMCThirdClearedDate is NULL
and SomaticMCFourthClearedDate is NULL
and DDS = @office
Order BY ISNULL( SomaticMCClearedDate, '31-Dec-2090')
--Psyc
insert into #temp
select SocialSecurityNumber, PsycMCClearedDate as ClearedDate, 'PM' as [Type], DDS, NULL
from ROCAPData where
PsycMCClearedDate between @Start and @End
and PsycMCSecondClearedDate is NULL
and PsycMCThirdClearedDate is NULL
and PsycMCFourthClearedDate is NULL
and DDS = @office
Order BY ISNULL( PsycMCClearedDate, '31-Dec-2090')

end

Select SocialSecurityNumber, ClearedDate, [Type], DDS, Count(*) As TotalCleared
from #temp
Group By SocialSecurityNumber, ClearedDate, [Type], DDS
Union
Select Null, Null, Null, Null, count(*)as TotalCleared
from #temp

END


Joseph

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-10 : 15:23:37
Do not do the SUM in the stored procedure, only return the two rows from it. Then in your reporting tool, perform the aggregate and display it wherever you'd like.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

Josephptran
Starting Member

14 Posts

Posted - 2009-04-10 : 16:04:19
Hello Tkizer,

I have more than 2 rows, because just want to make it simple. So I display only 2 rows. Sorry I am new developer, how can I aggregate and display it whereever I would like? would you please show me?

Thanks

Joseph
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-10 : 16:05:49
It doesn't matter how many rows there are. Have your stored procedure return just the detailed rows and then in your reporting tool add the aggregate to your report since this is purely a presentation issue.

What are you using for your reporting tool?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

Josephptran
Starting Member

14 Posts

Posted - 2009-04-10 : 16:17:14
reporting service, I guess.

here what I do, I create a store procedure and then go to ASP.NET 2.0 to display a report by using GridView. when you ask me, what reporting tool do I use? I am confused. You don't mind to explain it to me.

Thank you pretty women
Joseph

Joseph
Go to Top of Page

dsindo
Starting Member

45 Posts

Posted - 2009-04-21 : 20:58:49
select * from
(
Select SocialSecurityNumber, ClearedDate, [Type], DDS, Count(*) As TotalCleared
from #temp
Group By SocialSecurityNumber, ClearedDate, [Type], DDS
Union
Select Null, Null, Null, Null, count(*)as TotalCleared
from #temp
) a
order by SocialSecurityNumber desc
Go to Top of Page
   

- Advertisement -