boolean col count

Dear all,
Need to count numbers of rows where col1 = 1 for all listed cols

for example :
col1 , col2 , col3 , col4

I need the following result :
col1 , col2 , col3 , col4
3      ,   2    ,  0      , 6

is there a way to get such a result using 1 sql statement without union all.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:

Select Count(Max(col1)) AS Col1, Count(Max(col2)) AS Col2, Count(Max(col3)) AS Col3, Count(Max(col4)) AS Col4 FROM TableName

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ethar1Author Commented:
Thanks Lokesh B R, for your reply , but  for sure that will give an error
Lokesh B RDeveloperCommented:

what is the error?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Vitor MontalvãoMSSQL Senior EngineerCommented:
Try this one:
			WHEN 1 THEN 1
			ELSE 0
			END) TotalCol1,
			WHEN 1 THEN 1
			ELSE 0
			END) TotalCol2,
			WHEN 1 THEN 1
			ELSE 0
			END) TotalCol3,
			WHEN 1 THEN 1
			ELSE 0
			END) TotalCol4
FROM TableName

Open in new window

Jim HornMicrosoft SQL Server Data DudeCommented:
>Need to count numbers of rows where col1 = 1 for all listed cols
<Potentially stupid question>  Wouldn't each number be the same, as where col1 = 1 it would be the same number of rows for all columns?

Please provide a data mockup of what you're trying to pull off here.
How about this?

SELECT SUM(Col1 = 1) AS Col1TotTrue, SUM(Col2 = 1) AS Col2TotTrue, SUM(Col3 = 1) AS Col3TotTrue, etc...
FROM YourTable
ethar1Author Commented:
Dear all , thanks all for your reply.
I am talking about count not sum...
anyway I use union all.

Jim Horn , Indeed vary jerk reply.

Did you test these replies?  They SHOULD work.  But if they didn't work, let us know what the results were (and if they did not work, you do not have to accept them as solutions).

Both my reply and Vitor Montalvão's are based on Boolean expressions:

The expression "Col1 = 1" will return 1 if that expression is true, and zero if that expression is false.  So SUM(Col1 = 1) adds 1's for all records where Col1=1 and 0's for all records where Col1 is not 1,   Summing like this effectively gives you a COUNT of records where Col1 = 1.  

Vitor Montalvão's response is based on a similar principle.

<<Jim Horn , Indeed vary jerk reply.>>
Also, I believe you are misreading the spirit and intent of Jim's comment.  He was stating his understanding of your question and asking for additional details -- with the goal of helping you get this issue solved.  

"<Potentially stupid question> "

If you took offense to this part of the comment, that was in reference to his own question, not yours.  His comment was in no way insulting you or your question.
Jim HornMicrosoft SQL Server Data DudeCommented:
>where col1 = 1 for all listed cols
The question said col1 = 1, and not col2 = 1, col3 = 1, col4 = 1, etc., so my comment was to point that out and ask for a clarification.

Keep in mind that most of the people that post here will be native English-speaking US or European experts, so if you are offended with any of our comments you might want to stop and think if it's your misunderstanding before commenting.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.