Solved

Need to count values when a column contains 2 possible values. Stored Procedure

Posted on 2008-06-18
2
306 Views
Last Modified: 2013-11-28
I have a column whose value can be either 'R' or 'O' indicating owneres and renters.

I need a sql statement that returns the total number of renters and owners in an 'Renters' column and
'Owners' column.

for Example:

Owners   Renters
1392        8372

Psuedocode:
Select from tablename
Count(Where fieldname value='O') as Owners,
Count(Where fieldname value='R') as Renters

FROM TableName

What is the syntax?

Thanks,
0
Comment
Question by:Dovberman
2 Comments
 
LVL 2

Accepted Solution

by:
chuckatwork earned 250 total points
ID: 21816018
Use a case statement

Select sum (case when when fieldname = 'O' then 1 else 0 end) as Owners,
sum (case when when fieldname = 'R' then 1 else 0 end) as Renters,
from tablename

-Chuck
0
 

Author Comment

by:Dovberman
ID: 21816103
That worked,

I just replaced the 'when when' with 'when'

Thanks,
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question