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 2005 Forums
 Transact-SQL (2005)
 how to reverse the sum of a column by store proc.

Author  Topic 

Josephptran
Starting Member

14 Posts

Posted - 2009-04-10 : 15:36:21
Hello
I have created a store procedure to display a report and it works fine. But there is a small problems, which I don't know how to solve it. here is a problem,

The store procedure that I wrote which displays the result like below:

SocialSecurityNumber ClearedDate Type DDS TotalCleared
2
111223333 1/1/2009 DE BO 1
444552896 1/5/2009 SM CT 1


Number "2" is sum of 1 + 1, it supposes to display at bottom of column "TotalCleared" (not on top).

DOES ANYONE HERE KNOW HOW TO REVERSE IT TO BOTTOM????

THANKS IN ADVANCE

BELOW IS MY STORE PROCEDURE

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:39:22
Duplicate topic, locking thread.

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
   

- Advertisement -