我是 TD 新手,需要一些关于以下查询的帮助。
我们有一个查询,其中 from 子句如下所示:
`*sel *
FROM
V_ACOR ACOR,
V_ORTR ORTR,
V_MSAC MSAC,
V_MSRC MSRC,
V_REOR REOR
WHERE
ACOR.ORDER_ID = ORTR.ORDER_ID
AND
(SELECT MIN(ARRIVE_DATE_ORTR) AS ARRIVE_DATE FROM V_ORTR )
BETWEEN MSAC.BEGIN_DATE AND MSAC.END_DATE
AND
(SELECT MIN(ARRIVE_DATE_ORTR) AS ARRIVE_DATE FROM V_ORTR )
BETWEEN MSRC.BEGIN_DATE AND MSRC.END_DATE
AND
(SELECT MIN(ARRIVE_DATE_REOR) AS ARRIVE_DATE FROM V_REOR )
BETWEEN MSAC.BEGIN_DATE AND MSAC.END_DATE
AND
(SELECT MIN(ARRIVE_DATE_REOR) AS ARRIVE_DATE FROM V_REOR )
BETWEEN MSRC.BEGIN_DATE AND MSRC.END_DATE*`
现在的问题是,所有情况下的 MIN(ARRIVE_DATE) 都给了我所有条目的最小值。我们的要求是根据 ORDER_ID 获取最小日期。因此,我们尝试使用以下代替 MIN(ARRIVE_DATE):
`(SELECT ARRIVE_DATE FROM V_ORTR
qualify row_NUMBER() over (PARTITION by ORDER_ID order by ARRIVE_DATE ASC) = 1
)BETWEEN MSAC.BEGIN_DATE AND MSAC.END_DATE`
这显示一个错误:
子查询中不允许使用有序分析函数。
我们也尝试了以下方法:
`*sel * from
(
sel A.*
from (sel * FROM V_ORTR
QUALIFY row_NUMBER() over (PARTITION by ORTR.ORDER_ID order by ORTR.ARRIVE_DATE ASC)=1
) A ,
sel B.*
from (sel * FROM V_REOR
QUALIFY row_NUMBER() over (PARTITION by REOR.ORDER_ID order by REOR.ARRIVE_DATE_ORDR_TR ASC)=1
)B ,
V_ACOR,
V_MSAC,
V_MSRC
WHERE
ACOR.ORDER_ID_ORDR = A.ORDER_ID
AND A.ARRIVE_DATE_ORTR BETWEEN MSAC.BEGIN_DATE AND MSAC.END_DATE
AND A.ARRIVE_DATE_ORTR BETWEEN MSRC.BEGIN_DATE AND MSRC.END_DATE
AND B.ARRIVE_DATE_REOR BETWEEN MSAC.BEGIN_DATE AND MSAC.END_DATE
AND B.ARRIVE_DATE_REOR BETWEEN MSRC.BEGIN_DATE AND MSRC.END_DATE
)*`
这也给出了以下错误:
语法错误,应为名称或 Unicode 分隔标识符或 '(' 介于 '.' 和 'sel' 关键字之间。