Friday, February 24, 2012

A query sentence return a puzzle result

the talbe row like this:

ID Name Scoe
11 Tome 20
12 Jack 30
11 Tome 40
12 Jack 10
13 John 10

My query command like this:

Select T1.Id,T1.Name,T2.math
from st T1
right join
(Select Id as Id2,Sum(Math) as Math from St group by id) T2
on T1.id=t2.id2
where t1.id = t2.id2

While the reuslt is :

Id Name Score

11 Tom 60

11 Tom 60

12 Jake 40

12 Jack 40

13 John 10

I am wonder :the T1 gives a table with six rows, the T2 gives a table with three rows, and I use RIGHT JOIN to connect the two table,the result should be a table with only three rows.I tried INNER JOIN, the result is same.

but why ? please help me !


This might be what you want?
select ID, Name, Sum(Score) as Math from St group by ID, Name

result;

ID Name Math ---- ---------------- ---- 12 Jack 4013 John 1011 Tome 60(3 row(s) affected)

No comments:

Post a Comment