Having "HAVING" problems with GROUP BY.

From: Joseph Ranseth <jranseth_at_worldNO_SPAMcupfishing.com>
Date: Tue, 5 Sep 2000 10:59:09 -0500
Message-ID: <%J8t5.843$kU4.2388_at_news1.mts.net>


My query is quite complex, and it also not working properly! *g* From 2 tables, I am selecting records grouped by player_id where a certain number of conditions are met (ie: date_caught, etc.) I need to perform 2 GROUP BYs. 1 to ensure that the SUM of the actual_points is within a certain value, and the 2nd to ensure that each player has at least 4 entries.

I am only choosing the top four, which isn't a problem for players with 4 or more entries, but the group by is NOT working for players who have less than 4 entries. It is NOT leaving out the players with 3 or less entries......ANY HELP WOULD BE GREATLY APPRECIATED.

Here's the SQL (i've tried to snip it as much as I could....):

SELECT player_id , ROUND(SUM(actual_points)) as points FROM ( SELECT * FROM (SELECT player_id
, actual_points
, row_number() OVER (PARTITION BY player_id ORDER BY actual_points desc)
rank
, date_caught

FROM (SELECT *
FROM (SELECT ce.entry_id
, ce.date_caught as date_caught
, ce.actual_points as actual_points
, ce.player_id as player_id

FROM fantasy_entries ce )
UNION
SELECT *
FROM( SELECT ce.entry_id
, ce.date_caught as date_caught
, ce.actual_points as actual_points
, players.player_id as player_id

FROM catch_entries ce, dogfish.players
WHERE ce.pin = players.pin )
)
)
WHERE rank < 5
AND player_id IN ( SELECT *
FROM (SELECT player_id
FROM fantasy_entries
UNION
SELECT *
FROM (SELECT player_id
FROM (SELECT pl.player_id as player_id
, ce.pin

FROM players pl
, catch_entries ce

WHERE pl.pin = ce.pin)
)
)
GROUP BY player_id HAVING COUNT(actual_points) = 4) /*(when this is '> 0', it works okay, it just doesn't filter the players with more than 4 entries)*/
)
GROUP BY player_id HAVING SUM(actual_points) BETWEEN 0 AND 2800 ORDER BY points desc

Thanks in advance....I really appreciate any insight. (I've been fighting with this for a week!)

--
Joseph Ranseth - Webmaster
World Cup Fishing
http://www.worldcupfishing.com
Received on Tue Sep 05 2000 - 17:59:09 CEST

Original text of this message