SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Get the count
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

razeena
Yak Posting Veteran

53 Posts

Posted - 04/18/2013 :  05:00:13  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 04/18/2013 :  05:14:38  Show Profile  Reply with Quote

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

razeena
Yak Posting Veteran

53 Posts

Posted - 04/22/2013 :  01:56:23  Show Profile  Reply with Quote

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

India
52317 Posts

Posted - 04/22/2013 :  04:28:12  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000