﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / SQL Server Newbies  / select count / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 17:38:59 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: select count</title><link>http://www.sqlservercentral.com/Forums/Topic582379-1291-1.aspx</link><description>Of course it's possible:[code="sql"]use tempdbgocreate table dbo.testresults (	username nvarchar(128)not null,	answer1 varchar(10) null,	answer2 varchar(10) null,	answer3 varchar(10) null,	answer4 varchar(10) null,	answer5 varchar(10) null,	answer6 varchar(10) null,	answer7 varchar(10) null,	answer8 varchar(10) null,	answer9 varchar(10) null,	answer10 varchar(10) null);insert dbo.TestResults(username, answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10)select 'test', 'wrong', 'wrong', 'wrong', 'wrong', 'wrong', 'wrong', 'wrong', 'wrong', 'wrong', 'wrong'insert dbo.TestResults(username, answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8, answer9, answer10)select 'test2', 'wrong', 'right', 'right', 'right', 'wrong', 'wrong', 'wrong', 'wrong', 'wrong', 'right'select 	tr.username,	total.score,	total.answersfrom dbo.testresults tr	cross apply (		select sum (answers.score) as score, count(*) as answers		from (				select case tr.answer1 when 'right' then 1 else 0 end as score				union all				select case tr.answer2 when 'right' then 1 else 0 end as score				union all				select case tr.answer3 when 'right' then 1 else 0 end as score				union all				select case tr.answer4 when 'right' then 1 else 0 end as score				union all				select case tr.answer5 when 'right' then 1 else 0 end as score				union all				select case tr.answer6 when 'right' then 1 else 0 end as score				union all				select case tr.answer7 when 'right' then 1 else 0 end as score				union all				select case tr.answer8 when 'right' then 1 else 0 end as score				union all				select case tr.answer9 when 'right' then 1 else 0 end as score				union all				select case tr.answer10 when 'right' then 1 else 0 end as score		) answers	) total[/code]BUT, as the others stated already, you realy should model your tables better.</description><pubDate>Mon, 01 Mar 2010 07:18:41 GMT</pubDate><dc:creator>R.P.Rozema</dc:creator></item><item><title>RE: select count</title><link>http://www.sqlservercentral.com/Forums/Topic582379-1291-1.aspx</link><description>This is not an optimal table design for doing what you want in SQL.    It would be easier if your table was like this:userName       answerNo         answertest                      1               righttest                      2               wrongtest                      3               rightThen you could use    select count(*) where answer = 'right'   or    select sum(case when answer = 'right' then 1 else 0 end)As it stands you have to check each column separately with case statements, and sum the results.select userName,         case when answer1 = 'right' then 1 else 0 end +         case when answer2 = 'right' then 1 else 0 end +         case when answer3 = 'right' then 1 else 0 end +         etc etc</description><pubDate>Wed, 08 Oct 2008 09:17:39 GMT</pubDate><dc:creator>The Dixie Flatline</dc:creator></item><item><title>RE: select count</title><link>http://www.sqlservercentral.com/Forums/Topic582379-1291-1.aspx</link><description>first thing that comes to mind is to unpivot to turn the columns into rows then group and count. I've already had issues with getting pivot/unpivot syntax correct, so I won't post any code.the issue here is the DB design, there shouldn't be a column for each answer, they should be on different rows. what if you had 20,30,100,1000,10000000 answers? that'd be a lot of columns.this post should altleast get someone else out there thinking of a full answer.</description><pubDate>Wed, 08 Oct 2008 09:15:00 GMT</pubDate><dc:creator>bcronce</dc:creator></item><item><title>select count</title><link>http://www.sqlservercentral.com/Forums/Topic582379-1291-1.aspx</link><description>Hello I am inserting into my table into the Columns answer1 to answer10 the string "right" or "wrong". This works fine.  for example with a username username  answer1  answer2  answer3  answer4  .........Test         right	  wrong	   right	   right 	Is there a possibility with select count to get the rate of right answers for the total : for example 5 of 10 answers right ? </description><pubDate>Wed, 08 Oct 2008 02:12:14 GMT</pubDate><dc:creator>poloarun</dc:creator></item></channel></rss>