I need a bit of help with Databases , at school we have to work on Fox Pro (I know it's old but what can I do) and one of the tasks says the following: "List the longest course from each of the teachers".
The tables we have are
Courses -> idc (primary key) , id , name , length
Teachers -> id (primary key) , name
Is there any way of doing this with the select command ? There would have to be 2 columns I believe in the end teachers.name and courses.name ... I mean the teacher would have to be unique and have next to it a max from courses but only from the ones he/she is teaching...
I have tried:
1)SELECT teachers.name,courses.name,MAX(courses.legth) FROM teachers,courses WHERE teachers.id=courses.id
---> Error
2)SELECT teachers.name,courses.name FROM teachers,courses WHERE courses.legth=(SELECT MAX(courses.length) FROM courses) AND courses.id=teachers.id
---> Only shows the longest course
3)Making a program something like:
BOF()
i=1
CALCULATE MAX(teachers.id) TO y
DO WHILE i<y
IF teachers.id=i
x=SELECT MAX(courses.legth) FROM courses,teachers WHERE teachers.id=i
LIST teachers.name,courses.name FOR courses.legth=x
SKIP
i=i+1
ENDIF
ENDDO
---> That one goes on forever with no result.
After some time I managed to do something about it but in a very ugly form , I'm sure there has to be a better way:
i=1
USE teachers
CALCULATE MAX(id) TO y
USE
DO WHILE i<=y
USE courses
CALCULATE MAX(length) FOR id=i TO x
USE
USE teachers
LIST name for id=i
USE
USE courses
LIST name for id=i AND length=x
USE
i=i+1
ENDDO
Thank you.