0

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?

3
  • 1
    Select all columns and see where the variations are coming from and add those other columns to the join condition
    – Bohemian
    Aug 14, 2013 at 1:22
  • I created sample tables on my local server with sample data and it works like a charm. Can you post some sample data that you have in both the tables?
    – Nilesh
    Aug 14, 2013 at 1:54
  • hi, how many records are in ur students tables? only 300 + ? or 10k+ ? Aug 14, 2013 at 1:56

1 Answer 1

1

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!

6
  • I will try this. However, here is something to further complicate the scenario. I need to omit the SECTION field in the ID, so that the NewCourseID is only COURSE+Teacher_ID (A teacher can teach multiple sections of the same course ID). Would it be possible to join the tables like that so as to pull all students attached to the CRS_SECT even if they are technically in the same "COURSE+Teacher_ID" group. Aug 14, 2013 at 11:51
  • Explain a little more please... You want to change de NewCourseID, you can have a problem there, what if you have more than one teacher, in the same course, in different sections? Your ID will not be unique, is that right? If it is... i can't get "Would it be possible to join the tables like that" like what? Aug 14, 2013 at 12:31
  • What I mean is: The teacher ID really needs be to be dbo.StaffSchedule.COURSE+'-'dbo.StaffSchedule.Teacher_ID. For example, If there are 10 sections of a COURSE 1001 and 2 teachers each teaching 5 different sections, the course ID would look like this: 1001-Smith & 1001-Jones. However, I would like to attach a student to 1001-Smith or 1001-Jones based on what is in the dbo.StudentSchedule table. So if section 5 of 1001 is taught by Smith, then all students in that section need to be attached to 1001-Smith. Aug 14, 2013 at 12:45
  • See now! may be that can help you Aug 14, 2013 at 13:19
  • I am using the query above and I am getting 1699 results. However, I should only be seeing 200. What could be causing this? Aug 15, 2013 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.