?
Solved

Joining two tables multiple times

Posted on 2005-05-14
6
Medium Priority
?
253 Views
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

etc...

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

Thanks,
Sam Shaw.
0
Comment
Question by:SamShaw
  • 3
  • 2
6 Comments
 
LVL 1

Author Comment

by:SamShaw
ID: 14002180
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
0
 
LVL 3

Expert Comment

by:r_a_j_e_s_h
ID: 14002409
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)
0
 
LVL 1

Author Comment

by:SamShaw
ID: 14002430
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:

(STUDENT)
StudentID | LName   | FName
------------------------------------
BLO0001   | BLOGGS | Fred
SMI0001   |  SMITH   | Tim


(STUDENTCLASS)
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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Accepted Solution

by:
dbeneit earned 480 total points
ID: 14002730
sam,

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')

0
 
LVL 1

Author Comment

by:SamShaw
ID: 14002758
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!

Thankyou!

Sam Shaw
0
 
LVL 9

Expert Comment

by:dbeneit
ID: 14002784
thanks, excuse me
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

807 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