[quoted text, click to view] Albert Frausto wrote:
> Result Table:
>
> grantID grantTitle score1 score2 Sum Score
> --------- ------------ --------- ---------- --------------
> 20 Title #2 25 50 75
> 21 Title #3 25 15 40
>
> I'm stumped on how to extract the two judges scores and add the fields to
> it's corresponding record based on Scores.grant_ID = Grants.grantID.
>
> How would I accomplish this?
In SQL 2000:
SELECT s.GrantID, s.GrantTitle,
Score1 = SUM(CASE WHEN s.Rank = 1 THEN s.score ELSE 0 END,
Score2 = SUM(CASE WHEN s.Rank = 2 THEN s.score ELSE 0 END,
SumScore = SUM(s.score)
FROM (
SELECT g.GrantID, g.GrantTitle, s.score,
Rank = (SELECT COUNT(*) + 1
FROM Scores s2
WHERE s2.GrantID = s.GrantID
AND s2.JudgeID = s.JudgeID
AND s2.ScoreID < s.ScoreID )
FROM Grants g
INNER JOIN Scores s
ON s.GrantID = g.GrantID
) s
GROUP BY s.GrantID, s.GrantTitle
In SQL2005 you can use ROW_NUMBER() OVER() to calculate the Rank
column.
Chris