我在Teradata 13.0中尝试运行查询时遇到错误如何编写此查询?我将这个oracle查询转换为Teradata查询:
select sb.name,
sb.address1,
sb.address2,
sb.city,
sb.stateprovince,
sb.postalcode,
sb.country,
sb.cid,
sb.item,
sb.mcnum,
sb.dmh,
sb.hy,
sb.firstname,
sb.lastname,
sb.email,
sb.monikerexec,
sb.res,
(select cid from (select * from abc.submissions where res is not null order by res asc) where name = sb.name and item <> sb.item and res < sb.res and rownum =1) as mins,
(select min(price) from ap.hist where name = sb.name) as minauc,
sb.cat,
sb.uni
from abc.submission sb
order by sb.item
答案 0 :(得分:1)
听起来问题不在于缺少别名(这是一个问题),而是在子查询中需要ORDER BY
以便适当的cid
可以在minauc
语句中的第一个子查询SELECT
中选择。
您可以使用ORDER BY
而不是执行QUALIFY
并尝试抓取第一条记录,它使用窗口函数来选择您想要的记录:
SELECT
sb.NAME,
sb.address1,
sb.address2,
sb.city,
sb.stateprovince,
sb.postalcode,
sb.country,
sb.cid,
sb.item,
sb.mcnum,
sb.dmh,
sb.hy,
sb.firstname,
sb.lastname,
sb.email,
sb.monikerexec,
sb.res,
sb2.cid AS mins,
(
SELECT min(price)
FROM ap.hist
WHERE NAME = sb.NAME
) AS minauc,
sb.cat,
sb.uni
FROM abc.submission sb
LEFT OUTER JOIN abc.Submission sb2 ON
sb.name = sb2.name AND
sb.item <> sb.item AND
sb2.res < sb.res AND
sb2.res IS NOT NULL
QUALIFY ROW_NUMBER() OVER (PARTITION BY sb.name ORDER BY res asc) = 1
这使用JOIN将记录组合在一起,然后,使用QUALIFY
仅允许通过具有最低res
的联接记录。