I want to write a query that lists the programs we offer at my university. A program consists of at least a major, and possibly an "option", a "specialty", and a "subspecialty". Each of these four elements are detailed with an code which relates them back to the major.
One major can have zero or more options, one option can have zero or more specialties, and one specialty can have zero or more sub specialties. Conversely, a major is permitted to have no options associated with it.
In the result set, a row must contain the previous element in order to have the next one, i.e. a row will not contain a major, no option, and a specialty. The appearance of a specialty associated with a major implies that there is also an option that is associated with that major.
My problem lies in how the data is stored. All program data lies in one table that is laid out like this:
+----------------+---------------+------+
| program_name | program_level | code |
+----------------+---------------+------+
| Animal Science | Major | 1 |
| Equine | Option | 1 |
| Dairy | Option | 1 |
| CLD | Major | 2 |
| Thesis | Option | 2 |
| Non-Thesis | Option | 2 |
| Development | Specialty | 2 |
| General | Subspecialty | 2 |
| Rural | Subspecialty | 2 |
| Education | Major | 3 |
+----------------+---------------+------+
Desired output will look something like this:
+----------------+-------------+----------------+-------------------+------+
| major_name | option_name | specialty_name | subspecialty_name | code |
+----------------+-------------+----------------+-------------------+------+
| Animal Science | Equine | | | 1 |
| Animal Science | Dairy | | | 1 |
| CLD | Thesis | Development | General | 2 |
| CLD | Thesis | Development | Rural | 2 |
| CLD | Non-Thesis | Development | General | 2 |
| CLD | Non-Thesis | Development | Rural | 2 |
| Education | | | | 3 |
+----------------+-------------+----------------+-------------------+------+
So far I've tried to create four queries that join on this "code", each selecting based on a different "program_level". The fields aren't combining properly though.
I can't find simpler than this :
EDIT : corrected typo "Speciality", it should work now.