如何将此sql server查询转换为Teradata查询。这在Teradata中不起作用。
select *,
(select top 1 endvalue
from #AUID
where #AUID.adate <= t.adate and #AUID.tid=t.tid
order by #AUID.adate desc) as AsD
from History_1 t
答案 0 :(得分:0)
当涉及子查询时,您不能只有*
。您应该使用tablename。*或别名。*(如果使用了别名)。
select t.*,
(select top 1 endvalue
from #AUID
where #AUID.adate <= t.adate and #AUID.tid=t.tid
order by #AUID.adate desc) as AsD
from History_1 t
但是,这会抛出 失败6916 TOP N语法错误:子查询 错误不支持前N个选项。
您可以尝试加入表格。
select
t.*,
a.endvalue
from
History_1 t
left outer join
#AUID a
on a.tid = t.tid
and a.adate <= t.adate
qualify row_number() over (partition by t.tid, t.adate order by a.adate desc) = 1; --this ensures you select only one row with max(adate)