0

表案例_ACTIONS

CASE_ID      SEQ_NUM      DATE_OPEN     DATE_DUE   CODE       DESCREPTION_J
153286253    64125995     9/21/2013     9/28/2013  23001954   有害事象連絡票
153286253    64125996     9/23/2013     9/29/2013  23001938   PMS-SAE/AE報告書

表 LM_ACTION_ITEM_TYPE

ACTION_TYPE_ID      ACTION_TYPE 
23001954           JP FU AE Report Frm
23001938           JP FU with pregnancy 

我想为“JP FU AE Report Frm”或“JP FU with怀孕”类型的最新记录选择DESCREPTION_J

我写的查询是:

select 
    description_j 
from 
    case_actions 
where  
    date_open = (select MAX(ca.date_open) 
                 from case_actions ca, LM_ACTION_ITEM_TYPE lat 
                 where ca.code = lat.action_type_id 
                   and lat.action_type IN ('JP FU AE Report Frm – Contact Record', 'JP FU AE Report Frm–Detail Invest. Frm','JP FU with pregnancy form') 
                   and ca.case_id = :P_LET_CASE_ID and ca.action_status = 1 
                   and ca.deleted is NULL)

它给了我不正确的数据。请帮忙。

4

2 回答 2

1

您必须将外部查询与内部查询相关联,否则您可能会得到只有共同日期的 case_actions(而不是子查询中的其他过滤器)。

使用此示例数据: 在此处输入图像描述

使用 case_id=153286253 您的查询将给出 bbb 和 ddd。但是 ddd 有一个不同的 case_id 和代码,我认为你不需要。

将 and ca.case_id = case_actions.case_id 添加到内部查询会得到 bbb,我相信这是您期望的结果。

于 2013-09-23T20:48:08.347 回答
0

首先,您应该始终给出整个表结构定义。无论如何,根据您的示例查询,我可以想到一些事情。尝试这个。

select ca.description_j 
  from case_actions ca
 where ca.date_open = (select MAX(ca.date_open) 
                      from lm_action_item_type lat 
                     where ca.code = lat.action_type_id 
                       and lat.action_type IN ('JP FU AE Report Frm Contact Record', 'JP FU AE Report Frm Detail Invest. Frm','JP FU with pregnancy form') 
                       and ca.case_id = :P_LET_CASE_ID 
                       and ca.action_status = 1 
                       and ca.deleted is NULL)
于 2013-09-24T04:54:59.067 回答