Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Hi I have three tablesPUPILSSTUDYINGPupilID : PupilName : SubjectID : Subject-------------------------------------------1------ : Joe Smith : 1001----- : Maths1------ : Joe Smith : 1002----- : Stats1------ : Joe Smith : 1003----- : GeographySUBJECTLISTSubjectID : Subject-- : TargetTYPE-----------------------------------1001----- : Maths---- : Mathematics1002----- : Stats---- : Mathematics1003----- : Geography : HumanitiesTARGETSPupilID : PupilName : TargetType--: Target------------------------------------------1------ : Joe Smith : Mathematics : A1------ : Joe Smith : Humanities--: C1------ : Joe Smith : English---- : BI want to returnPupilID : PupilName : SubjectID : Subject---: TARGET----------------------------------------------------1------ : Joe Smith : 1001----- : Maths---- : A 1------ : Joe Smith : 1002----- : Stats---- : A1------ : Joe Smith : 1003----- : Geography : CI triedSELECT PS.pupilID,PS.pupilname,PS.xsubject,T.Target FROM ((pupilsstudying PS INNER JOIN subjectlist SL ON ps.subjectID=SL.subjectID)INNER JOIN targets T ON SL.targettype=T.targettype) WHERE PS.PupilID=1but this didn't seem to match the targets properly and generated lots of extra linesI'm coding in VB.net using an Access databaseAny help appreciated, thanks
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2012-08-19 : 07:05:32
I am familiar with Access only in passing, so the following query may not compile correctly. But the idea is that you need to join on the PupilID as well.
SELECT ps.PupilID, ps.PupliName, ps.SubjectId, ts.TargetFROM PUPILSSTUDYING ps INNER JOIN SUBJECTLIST sl ON sl.SubjectID = ps.SubjectId AND sl.Subject = ps.Subject INNER JOIN TARGETS ts ON ts.PupliId = ps.PupilID AND ts.TargetType = sl.TargetTypeWHERE PS.PupilID=1
seekaye2
Starting Member
2 Posts
Posted - 2012-08-19 : 09:26:54
Many thanks.Without joining the UPNs it was returning records for other pupils as well, working properly now
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2012-08-19 : 10:22:02
the given suggestion will yield multiple match from last join. Are you sure you dont anymore fields to join with last table?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/