Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get the count

Author  Topic 

razeena
Yak Posting Veteran

54 Posts

Posted - 2013-04-18 : 05:00:13
Hi,
I have three tables related to Question Papers. The objective is to find out how many users attended these papers. Each question paper may contain different sections. Even if a person attended atleast one
section, the paper is considered as attended. Please see the sample
table and expected output.
=================================
tbl_Question
***********
QID title description
100 History History
101 Maths Maths
102 Hindi Hindi

tbl_QuestionSection
**********************
SectionID QID SectionName
1 100 IndianHistory
2 100 WorldHistory
3 100 StateHistory
4 101 Statistics
5 101 Probability
6 102 General
7 102 Descriptive


tbl_UserSection
*****************
UserSecID UserID QpSecID
1 500 2
2 500 3
3 501 1
4 502 1
5 503 5

==================================
Expected result
-----------------------------
QuestionPaper NoofAttendees
History 3
Maths 1
Hindi 0
-------------------------------

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-18 : 05:14:38
[code]
SELECT q.title,
COUNT(DISTINCT UserID) AS NoOfAttendees
FROM tbl_Question q
LEFT JOIN tbl_QuestionSection qs
ON qs.QID = q.QID
LEFT JOIN tbl_UserSection us
ON us.QpSecID = qs.SectionID
GROUP BY q.title
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

razeena
Yak Posting Veteran

54 Posts

Posted - 2013-04-22 : 01:56:23

Perfect! It worked.Thank you.


quote:
Originally posted by visakh16


SELECT q.title,
COUNT(DISTINCT UserID) AS NoOfAttendees
FROM tbl_Question q
LEFT JOIN tbl_QuestionSection qs
ON qs.QID = q.QID
LEFT JOIN tbl_UserSection us
ON us.QpSecID = qs.SectionID
GROUP BY q.title


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-22 : 04:28:12
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -