0

List the student number, subject code, year, and the term of the students who register to only ONE subject in 2012 session 3.

SELECT StudentNum, RSubjectCode, Year, SessionTerm 
 FROM <TABLE> 
WHERE Year ='2012' 
  AND SESSIONTERM = '3';

This selects everyone from year 2012 and session 3. How do I only find out the student who registered ONE subject only?

1
  • 2
    You should think of a better title for your question. That one is totally useless Commented Nov 5, 2012 at 11:43

1 Answer 1

3

Group by StudentNum, Year and SessionTerm. That way, you can count the number of record to only return those that have one record. Because of that grouping, you'll need to get max (or min) on the subject code. The max of 1 record will be just that only record, so the result will be right and the query will be fast, even though it may look odd.

SELECT 
  StudentNum, 
  max(RSubjectCode) as RSubjectCode, 
  Year, 
  SessionTerm 
FROM 
  <TABLE> 
WHERE 
  Year ='2012' 
  AND SESSIONTERM = '3'
GROUP BY
  StudentNum, 
  Year, 
  SessionTerm 
HAVING
  count(*) = 1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.