Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-18
2
Medium Priority
?
334 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
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…

972 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