Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 01-24-2005, 05:53 PM   PM User | #1
melissa820
New Coder

 
Join Date: Oct 2004
Posts: 64
Thanks: 2
Thanked 0 Times in 0 Posts
melissa820 is an unknown quantity at this point
Count and Group By Query

I need some help with creating a new count/ group by query.

This was my previous query which worked fine:

SELECT coursecode, section, term, year, count(*) AS enrollment FROM gradregister WHERE status='pending' GROUP BY coursecode, section ,term, year;

Here is the result:
| coursecode | section | term | year | enrollment |
+------------+---------+--------+------+------------+
| ACTG5P31 | 1 | Winter | 2005 | 37 |
| ACTG5P32 | 1 | Winter | 2005 | 33 |
| ACTG5P32 | 2 | Winter | 2005 | 20 |


The table GradRegister is a bunch of records for courses that students are taking, their marks etc.
There is another table called GradCourses which holds the records of course information.
I wanted to add in the course title from the GradCourses table to the previous query. I tried this but it didn't work:

SELECT r.coursecode, s.coursetitle, r.section, r.term, r.year, count(*) AS enrollment FROM gradregister r LEFT JOIN gradcourses s ON r.coursecode=s.coursecode WHERE r.status='pending' GROUP BY r.coursecode, r.section, r.term, r.year;

Which provided this result:

| coursecode | coursetitle . . . . . . . . . . . . . | section | term | year | enrollment |
+------------+-----------------------------+---------+--------+------+------------+
| ACTG5P31 | Advanced Management Accounting | 1 | Winter | 2005 | 37 |
| ACTG5P32 | Global Tax Management and Planning | 1 | Winter | 2005 | 66 |
| ACTG5P32 | Global Tax Management and Planning | 2 | Winter | 2005 | 40 |


Some of the enrolments are double of what they should be. Any ideas on what I am doing wrong?
melissa820 is offline   Reply With Quote
Old 01-25-2005, 09:25 PM   PM User | #2
raf
Master Coder


 
Join Date: Jul 2002
Posts: 6,589
Thanks: 0
Thanked 0 Times in 0 Posts
raf is on a distinguished road
i don't understand why you need an outer join ... why not an inner join?
__________________
Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html
raf is offline   Reply With Quote
Old 07-06-2006, 05:28 PM   PM User | #3
melissa820
New Coder

 
Join Date: Oct 2004
Posts: 64
Thanks: 2
Thanked 0 Times in 0 Posts
melissa820 is an unknown quantity at this point
I needed the left join just in case there are registration records that do not have corresponding course records. This can happen in our system.

Here is the query I ended up using:

Code:
SELECT 
a.coursecode, 
a.section, 
a.coursetitle, 
a.term, 
b.year, 
count(b.coursecode) AS enrollment 
FROM gradcourses a 
LEFT JOIN 
gradregister b 
ON a.coursecode=b.coursecode 
AND a.section=b.section 
AND a.term=b.term 
WHERE b.status='pending' 
GROUP BY 
a.coursecode, 
a.section, 
a.coursetitle, 
a.term, 
b.year 
ORDER BY 
a.term, 
b.year, 
a.coursecode, 
a.section

Last edited by guelphdad; 08-11-2006 at 06:36 PM..
melissa820 is offline   Reply With Quote
Old 07-06-2006, 08:29 PM   PM User | #4
guelphdad
Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: Guelph Ontario Canada
Posts: 1,984
Thanks: 3
Thanked 62 Times in 57 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
why post to the thead a year and a half later?
guelphdad is offline   Reply With Quote
Old 08-11-2006, 04:08 PM   PM User | #5
melissa820
New Coder

 
Join Date: Oct 2004
Posts: 64
Thanks: 2
Thanked 0 Times in 0 Posts
melissa820 is an unknown quantity at this point
Sorry I am not on these boards much anymore. A comment made on another thread asked me to provide the answers if I found them. So I went through some of my older posts and did that, just in case someone else had the same question later on.
melissa820 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:23 AM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.