Last night I have inexplicably missed 2 conditions /facepalm

Now my JOIN works ok, without multiple records -

CREATE OR REPLACE FUNCTION words_stat_games(
                in_social integer,
                in_sid    text
        ) RETURNS TABLE (
                out_gid    integer,
                out_reason text,
                out_state1 text,
                out_score1 integer,
                out_score2 integer
        ) AS
$func$
        SELECT
                g.gid,
                g.reason,
                CASE WHEN g.player1 = s.uid THEN g.state1 ELSE g.state2 END,
                CASE WHEN g.player1 = s.uid THEN g.score1 ELSE g.score2 END,
                CASE WHEN g.player1 = s.uid THEN g.score2 ELSE g.score1 END
        FROM    words_games g
        JOIN    words_social s ON (g.player1 = s.uid OR g.player2 = s.uid)
        WHERE   g.finished IS NOT NULL
        AND     s.social = in_social                          -- MISSED
CONDITION
        AND     s.sid = in_sid                                   -- MISSED
CONDITION
        ORDER BY g.finished DESC
        LIMIT   10;

$func$ LANGUAGE sql STABLE;

Reply via email to