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