Hi,
I'm having some difficulties with the following...
I have a table of persons (pId, name), one of qualifications (qId, name, level, sublevel) and a table of candidates (cId, qId, pId, date).
The candidates table links the persons and qualifications tables. The qualifications have a level and a sublevel because some of them can have more than one part (like a theory and then practical). The levels are backwards (level 4 is lover than 1).
For ease with levels having different numbers of requirements, the sublevels are done backwards towards 1, i.e. when you have level XX, sublevel 1, you have completed level XX.
I'm trying to return the highest qualification level that each person has fully acheived but don't seem to be able to get it.
Here is the output of all of the people who have finished a qualification:
Both Chopin and Rachmaninoff have completed two levels. Chopin's highest is 2, Rachmaninoff's 3.
Now, what i want to do is GROUP BY pId but, as you can see, I don't necessarily get the row that I want.
As I understand it, HAVING would operate on this after the GROUP BY and before ORDER BY and so wouldn't do this. The min() is not accessible from the WHERE clause. My efforts with subqueries have failed becuase they return more than one row.
... WHERE level=(SELECT min(level) FROM quals JOIN candidates GROUP BY pId) ...
I'd be really grateful if anyone was able to shed some light on whether I'm going about this in the wrong way (are my data structures causing this?) or whether I am overlooking something simple here. Is there a means of supplying data to your subqueries?
Thanks very much,
j
I'm having some difficulties with the following...
I have a table of persons (pId, name), one of qualifications (qId, name, level, sublevel) and a table of candidates (cId, qId, pId, date).
The candidates table links the persons and qualifications tables. The qualifications have a level and a sublevel because some of them can have more than one part (like a theory and then practical). The levels are backwards (level 4 is lover than 1).
For ease with levels having different numbers of requirements, the sublevels are done backwards towards 1, i.e. when you have level XX, sublevel 1, you have completed level XX.
I'm trying to return the highest qualification level that each person has fully acheived but don't seem to be able to get it.
Here is the output of all of the people who have finished a qualification:
Code:
SELECT persons.name, persons.pId, quals.name, quals.level
FROM persons JOIN candidates USING(pId) JOIN quals USING(qId)
WHERE quals.sublevel=1
ORDER BY persons.name
-->
name pId name level
Chopin 1 Grade 3 3
Chopin 1 Grade 2 2
Rachmaninoff 4 Grade 3 3
Rachmaninoff 4 Grade 4 4
Now, what i want to do is GROUP BY pId but, as you can see, I don't necessarily get the row that I want.
Code:
SELECT persons.name, candidates.pId, quals.name, min(quals.level) as highestGrade
FROM persons JOIN candidates USING(pId) JOIN quals USING(qId)
WHERE quals.sublevel=1
GROUP BY pId
ORDER BY persons.name
-->
name pId name highestGrade
Chopin 1 [COLOR=red]Grade 3 2[/color]
Rachmaninoff 4 [COLOR=red]Grade 4 3[/color]
As I understand it, HAVING would operate on this after the GROUP BY and before ORDER BY and so wouldn't do this. The min() is not accessible from the WHERE clause. My efforts with subqueries have failed becuase they return more than one row.
... WHERE level=(SELECT min(level) FROM quals JOIN candidates GROUP BY pId) ...
I'd be really grateful if anyone was able to shed some light on whether I'm going about this in the wrong way (are my data structures causing this?) or whether I am overlooking something simple here. Is there a means of supplying data to your subqueries?
Thanks very much,
j