SQL/Redshift错误-仅筛选值的第一次出现

SQL/Redshift错误-仅筛选值的第一次出现,sql,amazon-redshift,Sql,Amazon Redshift,我尝试使用以下SQL脚本返回第一次出现的值: SELECT a.store_id, b.store_name, c.prod_id FROM ( select a.store_id as sh, b.store_name, c.prod_id count(1) ct,row_number() over (partition by store_id order by store_id desc) row_num from stores a, store_details b , produc

我尝试使用以下SQL脚本返回第一次出现的值:

SELECT a.store_id, b.store_name, c.prod_id FROM
  (
select a.store_id as sh, b.store_name, c.prod_id
count(1) ct,row_number() over  (partition by store_id order by store_id 
desc) row_num
from stores a, store_details b , product c
where a.id=b.store_id  and a.prod_id = c.id and  b.id = 101
group by a.store_id as sh, b.store_name, c.prod_id,
)t
WHERE row_num = 1;
我犯了一个错误

无效操作:关系“a”不存在


我使用的是红移数据库。有人能帮忙吗。谢谢..

您正在从子查询中选择,并且它的别名是“T”,您不能从子查询外部引用子查询别名

SELECT t.store_id, T.store_name, T.prod_id 
FROM
  (
   select a.store_id as sh, b.store_name, c.prod_id
   count(1) ct,row_number() over  (partition by store_id order by store_id 
   desc) row_num
   from stores a, store_details b , product c
   where a.id=b.store_id  and a.prod_id = c.id and  b.id = 101
   group by a.store_id as sh, b.store_name, c.prod_id,
)T
WHERE t.row_num = 1;

您正在从子查询中选择,并且其别名为“T”,您不能从子查询外部引用子查询别名

SELECT t.store_id, T.store_name, T.prod_id 
FROM
  (
   select a.store_id as sh, b.store_name, c.prod_id
   count(1) ct,row_number() over  (partition by store_id order by store_id 
   desc) row_num
   from stores a, store_details b , product c
   where a.id=b.store_id  and a.prod_id = c.id and  b.id = 101
   group by a.store_id as sh, b.store_name, c.prod_id,
)T
WHERE t.row_num = 1;