Solved

Counts per record

Posted on 2006-11-23
7
218 Views
Last Modified: 2008-03-17
I'm having a little trouble thinking about how to do a query.

Let's say the first table has

QuestionNum
1

and in the second table there's this

QuestionNum AnswerChoice
1                    1
1                    1
1                    2
1                    1
1                    5

how could I end up with something that looks like this

QuestionNum Answer CountAnswer
1                     1          3
1                     2          1
1                     3          0
1                     4          0
1                     5          1

If anyone can help, thanks ahead of time :)
0
Comment
Question by:arvanhalleorg
  • 3
  • 2
  • 2
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18003301
select q.questionnum, a.answer, sum(case when qa.answerchoice is null then 0 else 1 end) as CountAnswer
from Questions q
cross join ( select 1 as answer union all select 2 union all select 3 union all select 4 union all select 5 ) as a
left join QuestionAnswers qa
  on qa.questionnum = q.questionnum
 and qa.answer = q.answer
group by q.questionnum, a.answer
0
 
LVL 15

Expert Comment

by:mcmonap
ID: 18003309
SELECT
  QuestionNum
  , Answer
  , COUNT(*) CountAnswer
FROM
  myTable
GROUP BY
  QuestionNum
  , Answer
0
 
LVL 15

Expert Comment

by:mcmonap
ID: 18003323
oops, missed the 0 answers off in my query...
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 2

Author Comment

by:arvanhalleorg
ID: 18003404
angelIII i get this error:

Invalid column name 'answer'.
i made 2 tables to test with, the Question and QuestionAnswers

Question has QuestionNum
QuestionAnswers has QuestionNum and AnswerChoice

am I missing something?
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 18003448

select q.questionnum, a.answer, sum(case when qa.answerchoice is null then 0 else 1 end) as CountAnswer
from Questions q
cross join ( select 1 as answer union all select 2 union all select 3 union all select 4 union all select 5 ) as a
left join QuestionAnswers qa
  on qa.questionnum = q.questionnum
 and qa.answerchoice = q.answer
group by q.questionnum, a.answer
0
 
LVL 2

Author Comment

by:arvanhalleorg
ID: 18003477
gives me invalid column name 'answer' line 1

where does the select 2 union all select 3 etc come from?
0
 
LVL 2

Author Comment

by:arvanhalleorg
ID: 18003585
ah got it, thanks :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

929 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now