Joining two tables multiple times

Posted on 2005-05-14
Last Modified: 2010-03-19
okay what i have is two tables as follows:

STUDENT(StudentID, LName, FName)
STUDENTCLASS(StudentID, Block, ClassCode)

what i want is a query that returns each student and what classes they are enrolled in for each 'block' (there are seven blocks, labelled A through G, each with different subjects available)

So an example might look like this:
StudentID|  LName  |FName |BlockA |BlockB |BlockC  |BlockD  |BlockE  |BlockF  |BlockG
BLO0001  | BLOGGS |  Fred  | IT201 | FT323 | AR151 | GE421 | HS420 | PE444 | EN101
SMI0001  | SMITH     |  Tim   | MA198| HS110| AR151 | GG252 | EN203 | HL211 | IT101


i am unsure of how to go abut this... any help would be appreciated

Sam Shaw.
Question by:SamShaw
    LVL 1

    Author Comment

    I forgot to add that if a student is not enrolled in any subject for that block, which can occur, it needs to reflect that by returning a NULL or something
    LVL 3

    Expert Comment

    Try this code (replace ur table name)

    SELECT     StudentClass.BlockA AS Expr1, StudentClass.BlockB AS Expr2, StudentClass.BLockC AS Expr3, StudentClass.BlockD AS Expr4,
                          Student.StudentID AS Expr5, Student.LName AS Expr6, Student.FName AS Expr7
    FROM         Student INNER JOIN
                          StudentClass ON Student.StudentID = StudentClass.StudentID

    Expr1      Expr2      Expr3      Expr4      Expr5      Expr6                                              Expr7                                              
    ---------- ---------- ---------- ---------- ---------- -------------------------------------------------- --------------------------------------------------
    IT201      IT202      IT203      IT204      BLO0001    Solomon                                            Prem
    CS001      CS002      NULL       CS003      BLO0002    Kannan                                             Sathis

    (2 row(s) affected)
    LVL 1

    Author Comment

    Sorry, i mustn't have made myself clear...

    The example table i provided was the expected OUTPUT of the query... what i have is something like the following:

    StudentID | LName   | FName
    BLO0001   | BLOGGS | Fred
    SMI0001   |  SMITH   | Tim

    StudentID | Block | ClassCode
    BLO0001   |    A    |  IT201
    BLO0001   |    B    |  FT323
    BLO0001   |    C    |  AR151
    BLO0001   |    D    |  GE421
    BLO0001   |    E    |  HS420
    BLO0001   |    F    |  PE444
    BLO0001   |    G    | EN101  
    SMI0001   |    A    | MA198
    SMI0001   |    B    | HS110
    SMI0001   |    E    | EN203
    SMI0001   |    F    | HL211
    SMI0001   |    G    | IT101

    and what i want to produce is something like this:

    BLO0001  | BLOGGS |  Fred  | IT201 | FT323 | AR151 | GE421 | HS420 | PE444 | EN101
    SMI0001  | SMITH     |  Tim   | MA198| HS110|            |            | EN203 | HL211 | IT101

    Sorry for the confusion...
    Sam Shaw

    LVL 9

    Accepted Solution


    SELECT S.StudentID, s.LName , s.FName, isnull(scA.ClassCode,'') as BlockA , isnull(scB.ClassCode,'') as  BlockB, isnull(scC.ClassCode,'') as  BlockC, isnull(scD.ClassCode,'') as  BlockD, isnull(scE.ClassCode,'') as  BlockE, isnull(scF.ClassCode,'') as  BlockF, isnull(scG.ClassCode,'') as  BlockG
    from STUDENT S
    left join STUDENTCLASS ScA on (S.StudentID =ScA.StudentID  and ScA.Block ='A')
    left join STUDENTCLASS ScB on (S.StudentID =ScB.StudentID  and ScB.Block ='B')
    left join STUDENTCLASS ScC on (S.StudentID =ScC.StudentID  and ScC.Block ='C')
    left join STUDENTCLASS ScD on (S.StudentID =ScD.StudentID  and ScD.Block ='D')
    left join STUDENTCLASS ScE on (S.StudentID =ScE.StudentID  and ScE.Block ='E')
    left join STUDENTCLASS ScF on (S.StudentID =ScF.StudentID  and ScF.Block ='F')
    left join STUDENTCLASS ScG on (S.StudentID =ScG.StudentID  and ScG.Block ='G')

    LVL 1

    Author Comment

    dbeneit: your query for some reason returns WAAAY too many rows, like 64 copies of each row! i simply put a DISTINCT between SELECT and S.StudentID, and now it works fine!


    Sam Shaw
    LVL 9

    Expert Comment

    thanks, excuse me

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Introduced in Microsoft SQL Server 2005, the Copy Database Wizard ( is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now