简体   繁体   中英

SQL Count Records Held in All-time Records from a personal best table

I have got a Personal best table that holds fishing weight personal best records for each named fish. These fall under a particular species.

SO far I have managed to pull in the all time records for each named fish, grouped by the species.

What I am trying to do now is set up a statistics page that shows various info, and one being how many (count) all time records are held by the selected Fisherman.

I have a selector so you can choose the name to bring up the statistics, and this is working fine.

I can't seem to count selected all-time records by a selected fisherman.

I have tried nesting Select statements but to no conclusion. I am pretty new to this stuff also.

This is my starter code with the field names in question.

SELECT Specie, NameFish, Contender, MAX(Drams) FROM PB_Scores GROUP BY NameFish

and lets say the table is…

SPECIE - NAMEFISH - CONTENDER - DRAMS

Tench  | Barry    | Jonhson   | 234
Pike   | Glub     | Johnson   | 3456
Pike   | Blake    | Johnson   | 4567
Pike   | Blake    | Mitchell  | 4890
Pike   | Glub     | Mitchell  | 0 
Tench  | Barry    | Mitchell  | 2420

The result for selecting Mitchell should then be:-

2 (because Mitchell has got Barry and Blake at bigger weights so the count is 2).

Any help would be gratefully appreciated.

Doing a sub query to get the max weight for each species / name of fish, then joining that back to the table to get the contenders who have a fish of that species / name and weight:-

SELECT a.Contender, COUNT(*)
FROM PB_Scores a
INNER JOIN
(
    SELECT Specie, NameFish, MAX(Drams) AS MaxSpeciesWeight
    FROM PB_Scores
    GROUP BY Specie, NameFish
) b
ON a.Specie = b.Specie
AND a.NameFish = b.NameFish
AND a.Drams = b.MaxSpeciesWeight
GROUP BY a.Contender

SQL fiddle:-

http://www.sqlfiddle.com/#!2/966bf/1

Note this might give odd results if 2 people have both caught a fish of the same species, name and weight.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM