我有一种情况,我需要在一个类别的时间段内订购一定数量的积分,然后进入前2,因为您只能计算一个时期内的两个最高点总数。目前我的查询看起来像这样,但它不起作用。目前只是在选择2之后对结果进行排序。
SELECT TOP 2 *
FROM player as p
INNER JOIN player_event as pe ON p.PID=pe.PID
INNER JOIN event as e ON pe.EID=e.EID
WHERE e.ESeasonID=171801 AND e.ECat='Cup/Challenge'
ORDER BY pe.PEPts DESC
答案 0 :(得分:0)
你可以使用限制2
来做到这一点 SELECT *
FROM player as p
INNER JOIN player_event as pe ON p.PID=pe.PID
INNER JOIN event as e ON pe.EID=e.EID
WHERE e.ESeasonID=171801 AND e.ECat='Cup/Challenge'
ORDER BY pe.PEPts DESC LIMIT 2;
答案 1 :(得分:0)
结果集的排序通常发生在查询处理的最后。尝试使用窗口函数来获得所需内容:
SELECT *
FROM player as p
INNER JOIN player_event as pe ON p.PID=pe.PID
INNER JOIN event as e ON pe.EID=e.EID
WHERE e.ESeasonID=171801 AND e.ECat='Cup/Challenge'
QUALIFY ROW_NUMBER() OVER(ORDER BY pe.PEPts DESC) <=2
上面的查询应该适用于Teradata。如果您使用的是Postgres或其他不支持QUALIFY的系统,您可以尝试:
SELECT *
FROM (
SELECT
p.*, pe.*, e.*,
ROW_NUMBER() OVER(ORDER BY pe.PEPts DESC) AS RowNum
FROM player as p
INNER JOIN player_event as pe ON p.PID=pe.PID
INNER JOIN event as e ON pe.EID=e.EID
WHERE e.ESeasonID=171801 AND e.ECat='Cup/Challenge'
) src
WHERE RowNum <=2
ROW_NUMBER()OVER(由pe.PEPts订购)为每一行分配一个&#34;行号&#34;基于(ORDER BY ...)中指定的顺序。 Teradata中的QUALIFY等同于WHERE子句,但仅适用于窗口函数。
答案 2 :(得分:0)
此查询将根据本赛季的分数和所提供的类别为您提供前2名球员
syracuse