我正在尝试返回postgreSQL迭代的for循环的次数。
表:
players(season_year,firstname, lastname, season_win, season_loss, playoff_win, playoff_loss)
我正在尝试做的是使用聚合来查看哪个球员获得最多的赛季胜利(p.season_win + p.playoff_win - p.season_loss - p.playoff_loss)。使用这个聚合和赛季年份,我想要返回名字='x'lastname ='y'的等级玩家在赢/输差异中。但是,我似乎无法理解for循环是如何工作的。
以下是我在线查看的内容:
CREATE OR REPLACE FUNCTION get_player_rank(year INTEGER, firstn VARCHAR, lastn VARCHAR) RETURNS INTEGER AS $$
DECLARE player_rank INTEGER;
BEGIN
FOR player_rank IN
SELECT p.season_year, p.lastname, p.firstname, SUM(season_win + playoff_win - season_loss - playoff_loss) as total_wins
FROM player p
GROUP BY p.year, p.lastname, p.firstname
WHERE p.season_year = year LOOP
IF(firstn = p.firstname AND lastn = p.lastname) THEN
RETURN player_rank;
END IF
END LOOP
END
$$ LANGUAGE plpgsql;
我非常感谢能得到的任何帮助! 谢谢!
答案 0 :(得分:0)
尝试这样的事情:
-- ...
RETURN NEXT player_rank; -- return current row of SELECT
END IF
END LOOP
-- ...
有关返回的更多信息,请参阅此处:http://www.postgresql.org/docs/9.1/static/plpgsql-control-structures.html