我想要每个日期部分的最大音量,但是当我运行它时显示

时间:2016-09-02 20:32:41

标签: sql postgresql greatest-n-per-group

SELECT *
FROM
(SELECT
"public".steponesection.datesection,
"public".steponesection."Trade Price",
max ("public".steponesection."Trade Volume") OVER (partition by "public".steponesection.datesection) as max_Volm

FROM "public".steponesection)t

WHERE "public".steponesection."Trade Volume" = max_Volm

导致此错误:

[Err] ERROR:  missing FROM-clause entry for table "steponesection"
LINE 10: WHERE "public".steponesection."Trade Volume" = max_Volm

       ^

1 个答案:

答案 0 :(得分:0)

错误消息非常明确:where子句无法引用steponesection,因为在该级别的查询中没有具有该名称的标识符。派生表称为t,这就是您需要使用的表。

您也没有在内部查询中选择"Trade Volume",因此它在外层无法使用:

SELECT *
FROM (
   SELECT "public".steponesection.datesection,
          "public".steponesection."Trade Price",
          "public".steponesection."Trade Volume",  --<< missing
           max ("public".steponesection."Trade Volume") OVER (partition by "public".steponesection.datesection) as max_volm
   FROM "public".steponesection
) t
WHERE t."Trade Volume" = t.max_volm --<< use the alias of the derived table

我强烈建议避免使用引用的标识符,例如"Trade Price"。从长远来看,它们更麻烦,然后它们值得