如果您正在寻找包含来自的值的行MAX(num) GROUP BY id
,这往往是一种常见的模式......
WITH
sequenced_data
AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY id ORDER BY num DESC) AS sequence_id,
*
FROM
yourTable
)
SELECT
*
FROM
sequenced_data
WHERE
sequence_id = 1
编辑
我不知道 TeraData 是否会允许这样做,但逻辑似乎是有道理的......
SELECT
*
FROM
yourTable
WHERE
num = MAX(num) OVER (PARTITION BY id)
或者可能...
SELECT
*
FROM
(
SELECT
*,
MAX(num) OVER (PARTITION BY id) AS max_num_by_id
FROM
yourTable
)
AS sub_query
WHERE
num = max_num_by_id
这与我之前的回答略有不同;如果多条记录与相同的MAX(num)
,这将返回所有记录,另一个答案将只返回一个。
编辑
在您提出的 SQL 中,错误与该OVER()
子句包含不在您的 GROUP BY 中的字段这一事实有关。这就像尝试这样做...
SELECT id, num FROM yourTable GROUP BY id
num
是无效的,因为该字段中可以有多个值返回的每一行(返回的行由 定义GROUP BY id
)。
同理,你不能放在子句num
里面。OVER()
SELECT
id,
MAX(num), <-- Valid as it is an aggregate
MAX(num) <-- still valid
OVER(PARTITION BY id), <-- Also valid, as id is in the GROUP BY
MAX(num) <-- still valid
OVER(PARTITION BY num) <-- Not valid, as num is not in the GROUP BY
FROM
yourTable
GROUP BY
id
当您无法在OVER()
子句中指定某些内容时,请参阅此问题,以及显示何时(我认为)可以的答案:over-partition-by-question