dbTalk Databases Forums  

Stored procedure get count

microsoft.public.sqlserver.programming microsoft.public.sqlserver.programming


Discuss Stored procedure get count in the microsoft.public.sqlserver.programming forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
icanhelp33@gmail.com
 
Posts: n/a

Default Stored procedure get count - 08-09-2012 , 06:51 AM






I have a table CourtCase with following fields:
CourtCaseId (PK int)
CourtLevelId (FK int)
CourtId (FK int)
CaseNumber (varchar)
isDecided (bool)
isDeleted (bool)

I have another table CourtLevel with following fields
CourtlevelId, CourtTitle

I have a stored procedure which takes 2 parameters:
CourtLevelId, CourtId

I have to get the following data based on courtlevelid and courtid
CourtTitle, TotalCase

I need to get following details based on CourtlevelId and CourtId
Courttitle, TotalCases, Total Cases where isdecided=1, total cases where isdecided=1 and isdeleted=0

Reply With Quote
  #2  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Stored procedure get count - 08-09-2012 , 09:37 AM






icanhelp33 (AT) gmail (DOT) com wrote:
Quote:
I have a table CourtCase with following fields:
CourtCaseId (PK int)
CourtLevelId (FK int)
CourtId (FK int)
CaseNumber (varchar)
isDecided (bool)
isDeleted (bool)

I have another table CourtLevel with following fields
CourtlevelId, CourtTitle

I have a stored procedure which takes 2 parameters:
CourtLevelId, CourtId

I have to get the following data based on courtlevelid and courtid
CourtTitle, TotalCase

I need to get following details based on CourtlevelId and CourtId
Courttitle, TotalCases, Total Cases where isdecided=1, total cases
where isdecided=1 and isdeleted=0
select Courttitle
, count(*) as TotalCases
, sum(case isDecided when 1 then 1 else 0 end) as TotalDecided
, sum(case when isDecided =1 and isdeleted=0 then 1 else 0 end) as
TotalDecidedDeleted
from CourtCase as c
join CourtLevel as l on c.CourtLevelId = l.CourtLevelId
where c.CourtLevelId = @CourtLevelId and CourtId = @CourtId
group by Courttitle

Reply With Quote
  #3  
Old   
icanhelp33@gmail.com
 
Posts: n/a

Default Re: Stored procedure get count - 08-14-2012 , 05:10 AM



This query works just fine. However it returns only one courtleveltitle. More than one courtleveltitle exist for @courtid and @courtlevelid

select 1 as courtlevel,l.courtleveltitle, 'abc' as url, COUNT(*) as total, SUM(case isdecided when 1 then 1 else 0 end)as decided, SUM(case isdecided when 0 then 1 else 0 end)as pending, SUM(case isdecided when 1 then 1 else 0 end)-SUM(case when c.isdecided=1 and right(c.DecisionOrderFile,1)='/'then 1 else 0 end) as uploadfrom CourtCase as c join CourtLevels as l on c..CourtLevelID=l.CourtLevelID where c.CourtLevelID=@CourtLevelid and c.CourtID=@CourtId group by l.courtleveltitle

Reply With Quote
  #4  
Old   
Bob Barrows
 
Posts: n/a

Default Re: Stored procedure get count - 08-14-2012 , 05:33 AM



icanhelp33 (AT) gmail (DOT) com wrote:
Quote:
This query works just fine. However it returns only one
courtleveltitle. More than one courtleveltitle exist for @courtid and
@courtlevelid

select 1 as courtlevel,l.courtleveltitle, 'abc' as url, COUNT(*) as
total, SUM(case isdecided when 1 then 1 else 0 end)as decided,
SUM(case isdecided when 0 then 1 else 0 end)as pending, SUM(case
isdecided when 1 then 1 else 0 end)-SUM(case when c.isdecided=1 and
right(c.DecisionOrderFile,1)='/' then 1 else 0 end) as uploadfrom
CourtCase as c join CourtLevels as l on c.CourtLevelID=l.CourtLevelID
where c.CourtLevelID=@CourtLevelid and c.CourtID=@CourtId group by
l.courtleveltitle
Please show sample data in tabular format followed by results desired from
that sample data, also in tabular format.

Reply With Quote
  #5  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Stored procedure get count - 08-14-2012 , 09:06 AM



Quote:
This query works just fine. However it returns only one courtleveltitle.
More than one courtleveltitle exist for @courtid and @courtlevelid


select 1 as courtlevel,l.courtleveltitle, 'abc' as url, COUNT(*) as total,
SUM(case isdecided when 1 then 1 else 0 end)as decided,
SUM(case isdecided when 0 then 1 else 0 end)as pending,
SUM(case isdecided when 1 then 1 else 0 end)-
SUM(case when c.isdecided=1 and right(c.DecisionOrderFile,1)='/'
then 1 else 0
end) as upload
from CourtCase as c
join CourtLevels as l on c.CourtLevelID=l.CourtLevelID
where c.CourtLevelID=@CourtLevelid
and c.CourtID=@CourtId
group by l.courtleveltitle
So what is the primary key of CourtLevels? It's difficult to assist with a
problem like this without table definitions and sample data. But if I should
work from the names, I would assume that CourtLevelID is the PK of
Courtlevels, and in that case, there can only be one court-level title.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.