在窗口功能中别名表?

时间:2019-06-11 21:32:27

标签: sql data-science data-analysis presto

我正在尝试在窗口函数中为表创建别名,但不确定我做错了什么,因为当我给别名添加别名时会出现无法解析列的错误

    SELECT e.city,
       e.time,
       e.day,
       e.id,
       m.id
FROM
  (SELECT *,
          rank() OVER (PARTITION BY e.id,
                                    e.bin
                       ORDER BY e.time ASC) rnk
   FROM table e
   JOIN table2 m 
   on m.id = e.id
   WHERE e.status = 'YES'
   AND e.day BETWEEN date '2019-05-06' and date '2019-05-08')
WHERE rnk = 1

1 个答案:

答案 0 :(得分:2)

您已在最外面的e中使用了select别名。但是,该别名在范围内没有任何内容。内在的from不会像那样“伸手”(而作用域则以另一种方式“伸手”)。

所以:

SELECT e.city, e.time, e.day, e.id
FROM (SELECT e.*,
             rank() OVER (PARTITION BY e.id, e.bin ORDER BY e.time ASC) as rnk
      FROM table e
      WHERE e.status = 'YES' AND
            e.day BETWEEN date '2019-05-06' and date '2019-05-08'
     ) e
-------^ here
WHERE rnk = 1