INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How CanI Tie This Two Queries Together?

How CanI Tie This Two Queries Together?

(OP)
Hey guys,

I've been trying to write this query that will report multiple counts and keep hitting brick walls. Basically, I can get the counts in individual queries, but I can't find a way to time them together.

My tables are called COURSE and REGISTRATION-INSTRUCTOR. They share a common field called course id. The course table looks like this:

COURSE ID COURSE NAME SUBJECT HOURS
0, ENG. LITERATURE, ENG, 4
1, GEOLOGY, SCI, 4
2, INTRO TO SQL, COMP, 4
44, WRITING, ENG, 5
559, PHYSICS, SCI, 5

Instructor TABLE

CLASS_SECTION_ID COURSE_ID INSTRUCTOR_ID
2330, 0, 3
2331, 0, 4
3133, 2, 5
3233, 2, 89


I need to report the following:

total number of courses - 5
total number of instructors per subject - 4


I tried inner joining the two tables on course_id, but that messes up by course count. Since there are multiple classes for the same course, the count is higher than 5 now.

I have these two queries

Quote:



(select count(subject) as course_count from course)





SELECT count(b.instructor_id) as instructor_count, a.subject
FROM Course a,
[Registration-Instructor] b
WHERE a.course_id=b.course_id

group by a.subject




The problem is I can't join these tables on course_id unless I define it in the select statements. And if I define it in the select statements then I have to include it in the group by which breaks down count down to a new level giving me multiple counts per course_id. Any suggestions?











RE: How CanI Tie This Two Queries Together?

What do you want the final result to look like? I don't see how you'd put the total course count into a result set with the count of instructors for each course.

Tamar

RE: How CanI Tie This Two Queries Together?

Is this what you're looking for?

CODE

SELECT count(Distinct b.instructor_id) as instructor_count, 
       Count(Distinct a.course_id) As course_count,
       Count(Distinct a.subject) As subject_count
FROM Course a, 
[Registration-Instructor] b 
WHERE a.course_id=b.course_id 

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close