Reputation: 1
I've never posted here, so hoping this goes well :). I am using SQL Server 2008 R2 and I am trying to extract Student/Course information from a database. Here is what I am dealing with:
Table dbo.StudentsSchedule
StudentsSchedule table contains the following fields: CRS_CODE CRS_SECT ID_NUMBER
Table dbo.StaffSchedule:
COURSE SECTION Teacher_ID
Here is what I am trying to accomplish:
I would like to combine the COURSE+SECTION CODE+Teacher_ID to use that as a Unique "New Course ID" and then attach a Student to it.
I've attempted this but I am getting way too many results. I am expecting around 300 but receiving over 10K+
SELECT dbo.StaffSchedule.COURSE+'-'+ dbo.StaffSchedule.SECTION+'-'+dbo.StaffSchedule.Teacher_ID) as [NewCourseID],
dbo.StudentSchedule.ID_NUMBER AS [StudentID],
from dbo.StaffSchedule INNER JOIN dbo.StudentSchedule ON
dbo.StaffSchedule.COURSE=dbo.StudentSchedule.CRS_CODE and
dbo.StaffSchedule.SECTION=dbo.StudentSchedule.CRS_SECT
Note that Teacher_ID can only exist once per COURSE+SECTION.
Any ideas? Am I doing this wrong?
Upvotes: 0
Views: 187
Reputation: 184
Lets try to see it
Table dbo.StaffSchedule:
COURSE SECTION Teacher_ID
1 1 23
1 1 24
1 3 55
1 3 24
Table dbo.StudentsSchedule:
CRS_CODE CRS_SECT ID_NUMBER
1 1 44
1 1 45
1 3 89
1 3 44
This code:
SELECT dbo.StaffSchedule.COURSE+'-'+ dbo.StaffSchedule.SECTION+'-'+dbo.StaffSchedule.Teacher_ID) as [NewCourseID], dbo.StudentSchedule.ID_NUMBER AS [StudentID]
from dbo.StaffSchedule INNER JOIN dbo.StudentSchedule ON
dbo.StaffSchedule.COURSE=dbo.StudentSchedule.CRS_CODE and
dbo.StaffSchedule.SECTION=dbo.StudentSchedule.CRS_SECT
will return:
NewCourseID StudentID
1-1-23 44
1-1-23 45
1-1-24 44
1-1-24 45
1-3-55 89
1-3-24 89
1-3-55 44
1-3-24 44
May be the only problem I see is that for the same StudentID you have more than one value. I don't know what are you specting, so this is hoy much I can help you.
Sorry for my bad english! I hope this can help!
EDITE FOR NEW COMMENT
Is really simple to not use de Section in the NewCourseId
SELECT dbo.StaffSchedule.COURSE+'-'+dbo.StaffSchedule.Teacher_ID) as [NewCourseID], dbo.StudentSchedule.ID_NUMBER AS [StudentID]
from dbo.StaffSchedule INNER JOIN dbo.StudentSchedule ON
dbo.StaffSchedule.COURSE=dbo.StudentSchedule.CRS_CODE and
dbo.StaffSchedule.SECTION=dbo.StudentSchedule.CRS_SECT
But... what will happen? The result will be:
NewCourseID StudentID
1-23 44
1-23 45
1-24 44
1-24 45
1-55 89
1-24 89
1-55 44
1-24 44
Look what happened whit the ID 1-24, it's look to has duplicated values, do you understand why? Look at the original tables again.
Sorry for my bad english!
Upvotes: 1