mardi 27 janvier 2015

sql join on range giving double row for single record

I needed to join three tables Result, ResultITems and GradeScale. When i do, i get double or two of the same row. I tried Creating the records in sqlfiddle but i get a different correct result. The schema i used in creating the tables in my local sqlite db is exactly the same, which is shown here.


The result table



CREATE TABLE Result (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
SubjectID INTEGER REFERENCES Subjects ( ID ) ON DELETE CASCADE,
SessionID INT REFERENCES Sessions ( ID ),
TermID INT REFERENCES terms ( ID ),
ClassID INTEGER REFERENCES Classes ( ID )
);


The resultItems table



CREATE TABLE ResultItems (
StudentID INTEGER,
ResultID INTEGER REFERENCES Result ( ID ) ON DELETE CASCADE,
Total DECIMAL( 10, 2 )
);


And the gradescale table



CREATE TABLE gradeScale
(ID INTEGER PRIMARY KEY AUTOINCREMENT, minscore tinyint NOT NULL
,maxscore tinyint NOT NULL
,grade char(1) NOT NULL
);


now when i execute this query below, i et double row for each record in the ResultItems table



Select ri.studentid, ri.Total,g.grade
From ResultItems ri

left join GradeScale g
ON ( ri.total >= g.minscore AND ri.total <= g.maxscore )

left join Result r on r.id=ri.resultid

WHERE r.sessionid = 4
AND
r.termid = 1
AND
r.classid = 9
ORDER BY grade ASC;


Please see the picture below to see what i mean sample output


and here is the sql fibble which i created http://ift.tt/1CuSlNI


why am i getting double rows in the output when i execute in my local db?


Aucun commentaire:

Enregistrer un commentaire