PostgreSQL:join + outer-join + sub-select与比较值的问题。

时间:2011-10-26 10:20:10

标签: sql postgresql having

我正在使用postresql,但我真的很擅长构建SQL查询。 我有这个查询,它的工作原理:

SELECT handhistories FROM handhistories 
JOIN pokerhands using (pokerhand_id)  
JOIN gametypes using (gametype_id)
RIGHT OUTER JOIN playerhandscashkeycolumns using (pokerhand_id)
     WHERE pokerhands.site_id=0  
     AND pokerhands.numberofplayers>=5 and  pokerhands.numberofplayers<=7
     AND (bigblind = 2 OR bigblind = 4 )
     AND player_id in 
        (SELECT player_id FROM playerhandscashkeycolumns GROUP BY player_id
         HAVING AVG(case didvpip when true then 100::real else 0 end) <= 20 )

但我也想限制最后一个“拥有”,所以它会是这样的,但是当然它不起作用。

SELECT handhistories FROM handhistories 
JOIN pokerhands using (pokerhand_id)  
JOIN gametypes using (gametype_id)
RIGHT OUTER JOIN playerhandscashkeycolumns using (pokerhand_id)
       WHERE pokerhands.site_id=0  
       AND pokerhands.numberofplayers>=5 and  pokerhands.numberofplayers<=7
       AND (bigblind = 2 OR bigblind = 4 )
       AND player_id in 
        (SELECT player_id FROM playerhandscashkeycolumns GROUP BY player_id
         HAVING AVG(case didvpip when true then 100::real else 0 end) <= 20
         AND  HAVING AVG(case didvpip when true then 100::real else 0 end) > 10 )

如何“保存”之后的值,所以我也可以从底部比较它? 谢谢大家。

2 个答案:

答案 0 :(得分:1)

BETWEEN会为你工作吗?

HAVING AVG(case didvpip when true then 100::real else 0 end) BETWEEN 10 AND 20

(顺便说一句:丑陋的SQL语法,重用AND关键字)

更新:还可用于简化查询的其余部分:

AND pokerhands.numberofplayers BETWEEN 5 AND 7
AND bigblind IN ( 2, 4 )

答案 1 :(得分:1)

这主要是 @wildplasser 已经指出的内容 ..减去BETWEEN的错误 ..加JOIN而不是IN构造,这在PostgreSQL中通常更快 ..更容易阅读

SELECT handhistories
FROM   handhistories
JOIN   pokerhands USING (pokerhand_id)  
JOIN   gametypes USING (gametype_id)
RIGHT  JOIN playerhandscashkeycolumns USING (pokerhand_id)
JOIN   (
    SELECT player_id
    FROM   playerhandscashkeycolumns
    GROUP  BY player_id
    HAVING avg(CASE WHEN didvpip THEN 100::real ELSE 0 END) >  10
    AND    avg(CASE WHEN didvpip THEN 100::real ELSE 0 END) <= 20
    ) p USING (player_id)
WHERE  pokerhands.site_id = 0  
AND    pokerhands.numberofplayers BETWEEN 5 AND 7
AND    bigblind IN (2,4);

您可以对某些列进行表格限定,例如pokerhands.site_id,而不是其他列,例如handhistories,您可能需要清除它。