Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-18
2
Medium Priority
?
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
chuckatwork earned 1000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

722 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