Solved

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

Posted on 2008-06-18
2
274 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
Comment Utility
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
Comment Utility
That worked,

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

Thanks,
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now