Home > Archive > MS SQL Server > November 2005 > SQL Server Query help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author SQL Server Query help
dilip.movva@gmail.com

2005-11-30, 1:23 pm

Hi,
I have a Student table and course table. Every student will
enroll in 3 courses. The strucutre of the table is

Student
-----------
Studentid,
Student Name,
Course1ID,
Course2ID,
Course3ID

I have the course table

Course
------------
CourseID,
CourseName


Can someone tell how to write a query to get the course Names for a
particular student.
Sampel output should be something like this.

StudentName1, CourseName1, CourseName2, CourseName3.

Thanks in advance
Dilip

SQL

2005-11-30, 8:23 pm

If a studen always has all 3 coursids then get rid of the left joins
and the coalesce

select s.StudentName,
coalesce(c1.CourseName,'N/A') as CourseName1,
coalesce(c2.CourseName,'N/A') as CourseName2,
coalesce(c3.CourseName,'N/A') as CourseName3
from student s left join Course c1 on s.Course1ID = c1.CourseID
left join Course c2 on s.Course2ID = c2.CourseID
left join Course c3 on s.Course3ID = c3.CourseID

http://sqlservercode.blogspot.com/

dilip.movva@gmail.com

2005-11-30, 8:23 pm

Thank you very much.

- Dilip

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com