ORA - 01003未解析语句 - 尝试从当前返回多行的子查询中选择不同的最大记录

时间:2015-05-05 13:50:02

标签: oracle stored-procedures max distinct

我的一个子查询返回多行。我要做的是将其更改为仅返回一行,并返回行以选择具有最大值的行。

目前,子查询看起来像这样:

             (select case when floor(DATEDIFF(''d'', ProvNotify.start_dt, NVL(ProvNotify.complete_dt,sysdate))) = 0 then 1 else floor(DATEDIFF(''d'', ProvNotify.start_dt, NVL(ProvNotify.complete_dt,sysdate))) end
                FROM priv_wf_worklist ProvNotify
               WHERE ProvNotify.priv_application_id = pa.priv_application_id
                 AND ProvNotify.task_id = 600
                 AND ProvNotify.role_id = 100
                 AND ProvNotify.complete_dt IS NOT NULL) as "# Provider Notification Days",

我已经尝试将其更改为:

  SELECT MAX(select case when floor(DATEDIFF(''d'', ProvNotify.start_dt, NVL(ProvNotify.complete_dt,sysdate))) = 0 then 1 else floor(DATEDIFF(''d'', ProvNotify.start_dt, NVL(ProvNotify.complete_dt,sysdate)))) end
                FROM priv_wf_worklist ProvNotify
               WHERE ProvNotify.priv_application_id = pa.priv_application_id
                 AND ProvNotify.task_id = 600
                 AND ProvNotify.role_id = 100
                 AND ProvNotify.complete_dt IS NOT NULL) as "# Provider Notification Days",

  SELECT DISTINCT MAX(select case when floor(DATEDIFF(''d'', ProvNotify.start_dt, NVL(ProvNotify.complete_dt,sysdate))) = 0 then 1 else floor(DATEDIFF(''d'', ProvNotify.start_dt, NVL(ProvNotify.complete_dt,sysdate)))) end
                FROM priv_wf_worklist ProvNotify
               WHERE ProvNotify.priv_application_id = pa.priv_application_id
                 AND ProvNotify.task_id = 600
                 AND ProvNotify.role_id = 100
                 AND ProvNotify.complete_dt IS NOT NULL) as "# Provider Notification Days",

它会编译,但后来我得到一个oracle错误。请帮我!非常感谢你:))

1 个答案:

答案 0 :(得分:0)

max()函数需要绕过你想要取最大值的值,在这种情况下它是你的CASE语句。

 (select max(case when floor(DATEDIFF(''d'', ProvNotify.start_dt, NVL(ProvNotify.complete_dt,sysdate))) = 0 then 1 else floor(DATEDIFF(''d'', ProvNotify.start_dt, NVL(ProvNotify.complete_dt,sysdate))) end)
                FROM priv_wf_worklist ProvNotify
               WHERE ProvNotify.priv_application_id = pa.priv_application_id
                 AND ProvNotify.task_id = 600
                 AND ProvNotify.role_id = 100
                 AND ProvNotify.complete_dt IS NOT NULL) as "# Provider Notification Days",