dbTalk Databases Forums  

How to select count records that "contain" data?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss How to select count records that "contain" data? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Terry Olsen
 
Posts: n/a

Default How to select count records that "contain" data? - 09-19-2007 , 04:45 PM






I have a table in SQL Server that looks like this:

Name Status
---------------------------
Steve Complete
Steve In Queue
John Pending
John Complete
John Complete
Tim In Queue

I need to throw a query at this that will return the number of
"Complete"'s that each person has. So the result would look like:

Name Completed
----------------------------
Steve 1
John 2
Tim 0

Can anyone help out with what a query statement would look like to get
the desired output?


*** Sent via Developersdex http://www.developersdex.com ***

Reply With Quote
  #2  
Old   
Spook
 
Posts: n/a

Default Re: How to select count records that "contain" data? - 09-19-2007 , 05:38 PM






I think this should work:


Select Name, Count (Status)
from <<tablename>>
where status ='complete'
group by Name
order by Name



"Terry Olsen" <tolsen64 (AT) hotmail (DOT) com> wrote

Quote:
I have a table in SQL Server that looks like this:

Name Status
---------------------------
Steve Complete
Steve In Queue
John Pending
John Complete
John Complete
Tim In Queue

I need to throw a query at this that will return the number of
"Complete"'s that each person has. So the result would look like:

Name Completed
----------------------------
Steve 1
John 2
Tim 0

Can anyone help out with what a query statement would look like to get
the desired output?


*** Sent via Developersdex http://www.developersdex.com ***



Reply With Quote
  #3  
Old   
Terry Olsen
 
Posts: n/a

Default Re: How to select count records that "contain" data? - 09-19-2007 , 05:47 PM



Yes, that works. But it doesn't show the row with no completes. So
unless a person has at least one complete, they won't show.

This will work for me, but is there a way to get it to include the
people with no completes?

Thanks again!


*** Sent via Developersdex http://www.developersdex.com ***

Reply With Quote
  #4  
Old   
Ed Murphy
 
Posts: n/a

Default Re: How to select count records that "contain" data? - 09-19-2007 , 06:19 PM



Terry Olsen wrote:

Quote:
Yes, that works. But it doesn't show the row with no completes. So
unless a person has at least one complete, they won't show.

This will work for me, but is there a way to get it to include the
people with no completes?
select Name, count(case Status when 'Complete' then 1 else 0 end)
from <<tablename>>
group by Name
order by Name


Reply With Quote
  #5  
Old   
Arto V Viitanen
 
Posts: n/a

Default Re: How to select count records that "contain" data? - 09-20-2007 , 01:29 AM



Ed Murphy wrote:
Quote:
Terry Olsen wrote:

Yes, that works. But it doesn't show the row with no completes. So
unless a person has at least one complete, they won't show.
This will work for me, but is there a way to get it to include the
people with no completes?

select Name, count(case Status when 'Complete' then 1 else 0 end)
from <<tablename
group by Name
order by Name
I guess you meant


select Name, sum(case Status when 'Complete' then 1 else 0 end)
from <<tablename>>
group by Name
order by Name

--
Arto Viitanen


Reply With Quote
  #6  
Old   
Ed Murphy
 
Posts: n/a

Default Re: How to select count records that "contain" data? - 09-20-2007 , 09:07 PM



Arto V Viitanen wrote:

Quote:
Ed Murphy wrote:
Terry Olsen wrote:

Yes, that works. But it doesn't show the row with no completes. So
unless a person has at least one complete, they won't show.
This will work for me, but is there a way to get it to include the
people with no completes?
select Name, count(case Status when 'Complete' then 1 else 0 end)
from <<tablename
group by Name
order by Name

I guess you meant


select Name, sum(case Status when 'Complete' then 1 else 0 end)
Yes, sorry.


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 - 2012, Jelsoft Enterprises Ltd.