Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I am retrieving records from database using select statement, but because i stored value property in the table i have to join multiple tables in order to display the text property, before joining the other tables, the result came out fine returning four rows, after joining the table i got 14 rows, i tried to debug it, i didnt seem to see where the issue is coming from. I will appreciate if anyone could assist.

What I have tried:

Script before joining tables:
SQL
SELECT DISTINCT 
    ,rs.[ResultID]
    ,rs.[StudentNo_]
    ,rs.[House]	
    ,rs.[DOB]
    ,rs.[AcademicSession]
    ,rs.[Term]
    ,rs.[TeacherRemark]
    ,rs.[MarkObtainable]
    ,rs.[MarkObtained]
    ,rs.[Subject]
    ,rs.[School] 	
    ,rs.[CATest1] 
    ,rs.[CATest2]
    ,rs.[CATest3]
    ,rs.[CATest4]
    ,rs.[CATest5]
    ,rs.[ExamScore]
    ,rs.[HeadTeacherRemark]
    ,rs.[CATotal]
    ,rs.[Grade]
    ,rs.[Remark]
    ,rs.[Class]
    ,rs.[ClassAverage]
    FROM [Results] rs    
    WHERE rs.[StudentNo_] = @StudentNumber AND rs.[Term] = @Term AND rs.  [AcademicSession] = @AcademicSession AND rs.[Class] = @Class AND rs.[School] =     @School

script after joining tables:
SQL
SELECT DISTINCT pss.[Code] SubCode
      ,ss.[Description] SubjectName
	  ,pss.[Subject Code] SubjectCode
	  ,pss.[Core Subject] CoreSubject
	  ,pss.[CA Calculator] CACalculator
	  ,pss.[Grade Display] GradeDisplay
	  ,pss.[Term Total Calculator] TermTotalCalculator
      ,rs.[ResultID]
      ,rs.[StudentNo_]
	  ,sd.[FullName]
	  ,sd.[ImagePath]
	  ,sd.[Gender]
      ,sd.[House]	  
      ,sd.[Date Of Birth] DOB
      ,ass.[Description] AcademicSession
      ,rs.[Term]
      ,rs.[TeacherRemark]
      ,rs.[MarkObtainable]
      ,rs.[MarkObtained]
      ,rs.[Subject]
      ,sl.[Name] School	  
      ,rs.[CATest1] 	  
      ,rs.[CATest2]
      ,rs.[CATest3]
      ,rs.[CATest4]
      ,rs.[CATest5]
      ,rs.[ExamScore]
      ,rs.[HeadTeacherRemark]
      ,rs.[CATotal]
      ,rs.[Grade]
      ,rs.[Remark]
      ,rs.[Class]
	  ,rs.[ClassAverage]
  FROM [Results] rs
  INNER JOIN [PrimarySubjectSetUp] pss ON rs.[Subject] = pss.[Subject Code]
  INNER JOIN [SchoolList] sl ON rs.[School] = sl.[Code]
  INNER JOIN [Student] sd ON rs.StudentNo_ = sd.No_
  INNER JOIN [School Subjects] ss ON pss.[Subject Code] = ss.[Code]
  INNER JOIN [AcademicSession] ass ON rs.[AcademicSession] = ass.Code
  WHERE rs.[StudentNo_] = @StudentNumber AND rs.[Term] = @Term AND rs.[AcademicSession] = @AcademicSession AND rs.[Class] = @Class AND rs.[School] = @School
  ORDER BY pss.[Subject Code] ASC
Posted
Updated 17-Jun-16 5:39am
v3
Comments
CHill60 16-Jun-16 11:03am    
Without some sample data then we can't really help with specifics. One of the tables has multiple rows for one of the matches is the answer ... you will have to look at your data to see which one
Uwakpeter 17-Jun-16 10:59am    
Thanks,that was the issue
ZurdoDev 16-Jun-16 11:25am    
We can't help other than saying you have multiple matching records in one of the new tables you joined to.
Uwakpeter 17-Jun-16 10:59am    
Thanks,that was the issue
RedDk 16-Jun-16 12:41pm    
I'll say this, just off hand, it's probably the type of JOIN. And in conclusion, check the table definitions for each of the tables and make sure all the types are identifiably the types that you want them to be. Sometimes imported/exported data will manifest wrong types in SQL Server.

1 solution

As mentioned in comments, the reason for the multiple records is because at least one of the tables you added in your joins has more than one matching record.

Glad to hear you got it worked out.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900