>我正在尝试为窗口函数中的表添加别名,但不确定我做错了什么,因为当我别名时,它会给出无法解析列的错误
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
您在最外层的select
中使用了e
别名。 但是,该别名的范围内没有任何内容。 内部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