在Oracle中选择具有最大日期的日期

时间:2017-01-18 08:02:48

标签: oracle dblink

我有使用db_link到MS SQL的表达式:

   select b."Str" as "State" ,a."_Fld9059" as "Date" from 
   "_InfoRg9050"@SQLSERVER.UISLAB.COM a INNER JOIN 
   "EnumTexts"@SQLSERVER.UISLAB.COM b
   on a."_Fld9052RRef" = b."_IDRRef"
   where a."_Fld10998" = '1104000009' and
   a."_Fld10998" = to_date(max(a."_Fld9059"),'dd.mm.yyyy')
   order by a."_Fld9059" desc;

我想上传最大日期的值。有谁能够帮我 ? 当我运行此查询时,我收到ORA-00934错误。

2 个答案:

答案 0 :(得分:2)

您遇到错误的直接原因是MAX()子句中出现WHERE。一种可能的解决方法(可能是您的意图)是使用WHERE子句中的子查询来标识最大日期:

SELECT b.Str AS State,
       a._Fld9059 AS Date
FROM _InfoRg9050 a
INNER JOIN EnumTexts b
    ON a._Fld9052RRef = b._IDRRef
WHERE a._Fld10998 = '1104000009' AND
      a._Fld10998 = (SELECT MAX(TO_DATE(_Fld9059, 'dd.mm.yyyy')) FROM _InfoRg9050)
ORDER BY a._Fld9059 DESC

但是,您不清楚为什么要将_InfoRg9050._Fld10998与字符串'1104000009' 进行比较。你需要自己解决这个问题,我相信会得到一个有意义的结果。

答案 1 :(得分:0)

感谢您的帮助。我懂了。

SELECT b."Str" AS "State"
FROM "_InfoRg9050"@SQLSERVER.UISLAB.COM a
INNER JOIN "EnumTexts"@SQLSERVER.UISLAB.COM b
ON a."_Fld9052RRef" = b."_IDRRef"
WHERE a."_Fld10998" = '1104000009' AND
a."_Fld9059" = (select MAX(a."_Fld9059") from "_InfoRg9050"@SQLSERVER.UISLAB.COM a
INNER JOIN "EnumTexts"@SQLSERVER.UISLAB.COM b
on a."_Fld9052RRef" = b."_IDRRef"     
where a."_Fld10998" = '1104000009')
ORDER BY a."_Fld9059" DESC