473,320 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Getting a count from multiple columns

1
I'm new to SQL Server 2005 and have a question.

I have a table with the following format:

Columns:
ID Col_A Col_B Col_C Col_D .... (Col_X)

Values:
1 Yes No No Yes
2 No Yes No No

..etc


For each unique ID, I'd like to get 2 values: The number of "Yes" values (Yes_Count) and the number of "No" Values (No Count)

In my stored procedure, I created a cursor so I can traverse through each of the rows in the table. However, I'm not sure where to go from here.

I've been struggling with this for a while - Any help would be appreciated!

ashah
Jan 31 '07 #1
1 2751
iburyak
1,017 Expert 512MB
Try this:

[PHP]SELECT ID,
case Col_A when 'Yes' then 1 else 0 end +
case Col_B when 'Yes' then 1 else 0 end +
case Col_C when 'Yes' then 1 else 0 end +
case Col_D when 'Yes' then 1 else 0 end +
case Col_X when 'Yes' then 1 else 0 end AS 'YES',

case Col_A when 'No' then 1 else 0 end +
case Col_B when 'No' then 1 else 0 end +
case Col_C when 'No' then 1 else 0 end +
case Col_D when 'No' then 1 else 0 end +
case Col_X when 'No' then 1 else 0 end AS 'NO'

FROM table_name[/PHP]


Good Luck
Jan 31 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: John Hunter | last post by:
I have a largish data set (1000 observations x 100 floating point variables), and some of the of the data are missing. I want to try a variety of clustering, neural network, etc, algorithms on the...
3
by: Dean | last post by:
I want to build query to return how many rows are in this query: select distinct c1, c2 from t1 But SQL won't accept this syntax: select count (distinct c1, c2) from t1 Does someone know how...
2
by: Joe | last post by:
Hi All, I am new to using the Access DB and I need some help if someone is able to give it to me. What I want to do is get the names of the columns of certain tables. Not the data in the table...
1
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting,...
3
by: | last post by:
I wrote a class in VB.NET to export the contents of a datagrid to Excel. It works perfectly on my machine, but it fails on my customers' PCs that have identical versions of Win XP (SP1) and Excel...
4
by: dallasfreeman | last post by:
I'm looking at a quick way to get results that are displayed as rows to display as columns. I have three tables:- - The Questions for the survey - The Results of the survey (Columns are listed...
1
by: macupryk | last post by:
I am not sure how to get a column from a datatable, I cannot use foreach. DataColumn dataColumn = DataColumn TEXTDT.Columns.ColumnName.Trim(); if (rows < TEXTDT.Rows.Count) // this will...
4
by: Mina Patel | last post by:
Hi trying to find the number of duplicate tuples/records in table based on multiple columns ie. select count(distinct list multiple column key ) from x where date = y HAVING ( COUNT(multiple...
1
by: k4 | last post by:
I have a database that contains a column for UnitName , BeginDate and EndDate. I want to pass two parameters (@BeginDate and @EndDate) and retrieve a table of values that include UnitName...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.