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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Only calculate some child records

Author  Topic 

dubem1
Starting Member

4 Posts

Posted - 2005-12-18 : 23:45:16
Hello

Imagine a athlete judged by 10 judges. This athlete will get 10 notes for his performance. Each note will be store in a table. (AthleteNote) I also have the athlete table.
The particularity is that the final note of the athlete is the sum of is 8 best notes.

What I try to acheieve it's a query that is returning the list of athlete with is fnal note.

I can't do a "Sum" on the note column because it will add all of the 10 notes.

So how can I just calculate de 8 best notes within a query?

Thank you
Martin

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2005-12-18 : 23:49:19
hi,
could you please post the table structure alongwith the sample data and the output you are looking for??
Go to Top of Page

dubem1
Starting Member

4 Posts

Posted - 2005-12-18 : 23:55:42
Table Athlete
-------------
1 Mark Smith
2 Jason Spencer

Table AthleteNote
-----------------
1 9
1 8
1 9.5
1 7
1 8
1 4
1 9
1 7.5
1 6.5
1 5
2 9
2 9
2 6.5
2 6
2 8
2 8
2 9
2 10
2 4.5
2 7

Output
------
Mark Smith 64.5
Jason Spencer 66.5
Go to Top of Page

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2005-12-19 : 00:25:24
i guess this is what u r looking for
select t.id, sum(t.note)
from atheletenote t
where t.note in(select top 8 note from atheletenote where id=t.id order by note desc)
group by t.id
Go to Top of Page

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2005-12-19 : 00:30:34
u can have an inner join with athlete table as well to get the names

select a.name, sum(t.note)
from athletenote t inner join athlete a
on a.id = t.id
where t.note in(select top 8 note from athletenote where id=t.id order by note desc)
group by a.name
Go to Top of Page
   

- Advertisement -