troubleshooting Question

Help with SQL

Avatar of HLRosenberger
HLRosenbergerFlag for United States of America asked on
Microsoft SQL ServerMicrosoft SQL Server 2005Microsoft SQL Server 2008
5 Comments1 Solution104 ViewsLast Modified:
I have a classroom table.  It has a primary key column, along with primary teacher Foreign key,  secondary teacher Foreign key and tertiary teacher Foreign key.  Each of the teacher FKs are keys to a user table.

A teacher could be primary in one room, secondary in another and tertiary in a third.  So, I need a query that returns the Primary Key of the classroom for a teacher, based on the primary, secondary and tertiary hierarchy.   For this example I would want the Primary Key of the classroom where they are the primary teacher.  

If the teacher was not a primary teacher in any classroom, but was secondary teacher in one room and tertiary in a second room, then I need the Primary Key of the classroom where they are the secondary teacher.  

classroom Data examples

classroom ID       FKPrimary       FKSecondary         FKTertiary
1                                  100                    NULL                     NULL
2                                  200                     1234                     400
3                                  300                     400                       500
4                                  400                     300                       200  
5                                  500                     400                       4444
                               

For teacher 100, I need classroom ID of 1.
For teacher 200, I need classroom ID of 2.
For teacher 300, I need classroom ID of 3.
For teacher 400, I need classroom ID of 4.
For teacher 500, I need classroom ID of 5.

For teacher 1234, I need classroom ID of 2.
For teacher 4444, I need classroom ID of 5.
ASKER CERTIFIED SOLUTION
Brian Crowe
Database Manager

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2005

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros