我有2个表:一个包含核心数据的主要APPLICATION表,以及一个反映APPLICATION表中核心数据状态变化的STATUSTRACKING表。
APPLICATION表和STATUSTRACKING表之间存在1:N的关系。示例如下:
-- APPLICATION
SELECT A.ID, A.DECISIONON, A.APPLICATIONSTATUS_ID AS CURRENTSTATUS
FROM APPLICATION A
WHERE A.ID=1099;
Results:
ID DECISIONON CURRENTSTATUS
1099 5/05/2009 5
-- STATUS TRACKING
SELECT ST.ID, ST.APPLICATION_ID AS APP_ID, ST.APPLICATIONSTATUS_ID AS STATUS, ST.CREATEDATE
FROM STATUSTRACKING ST
WHERE ST.APPLICATION_ID=1099
ORDER BY ST.CREATEDATE DESC;
Results:
ID APP_ID STATUS CREATEDATE
44466 1099 5 5/05/2009
44458 1099 7 5/05/2009
10826 1099 8 21/07/2008
9770 1099 7 9/07/2008
4410 1099 3 9/05/2008
3814 1099 2 2/05/2008
3803 1099 1 2/05/2008
问题: 我需要选择具有最近日期的STATUSTRACKING记录以获取特定状态。这是我的尝试:
SELECT A.ID AS APP_ID, A.APPLICATIONSTATUS_ID AS CURR_ST, Z.ID AS ST_ID, Z.CREATEDATE, Z.APPLICATIONSTATUS_ID AS OLD_ST
FROM APPLICATION A, STATUSTRACKING Z
WHERE A.APPLICATIONSTATUS_ID in (5,6)
AND A.ID IN (337,1099,1404,9441)
AND Z.APPLICATIONSTATUS_ID = 7
AND Z.APPLICATION_ID=A.ID
ORDER BY A.ID ASC, Z.CREATEDATE DESC;
Results:
APP_ID CURR_ST ST_ID CREATEDATE OLD_ST
337 6 13978 17/08/2008 7
1099 5 44458 5/05/2009 7
1099 5 9770 9/07/2008 7
1404 6 15550 28/08/2008 7
9441 5 49271 3/06/2009 7
9441 5 46058 13/05/2009 7
问题是重复的行。我必须只显示MOST RECENT CreateDate的行。在App_ID 1099的情况下,它将是此记录:
1099 5 44458 5/05/2009 7
我显然不想排除那些没有重复的行。
我认为我在这条声明的正确轨道上给出了我正在寻找的行:
SELECT A.ID, A.APPLICATION_ID, A.APPLICATIONSTATUS_ID, A.CREATEDATE
FROM (SELECT *
FROM STATUSTRACKING
WHERE APPLICATIONSTATUS_ID=7
AND APPLICATION_ID=1099
ORDER BY CREATEDATE DESC) A
WHERE ROWNUM = 1;
...但我似乎无法使用我的主要选择声明。
我想要的结果集应如下所示:
APP_ID CURR_ST ST_ID CREATEDATE OLD_ST
337 6 13978 17/08/2008 7
1099 5 44458 5/05/2009 7
1404 6 15550 28/08/2008 7
9441 5 49271 3/06/2009 7
etc. ...
我不是Oracle / SQL专家,所以任何帮助都会受到赞赏。
答案 0 :(得分:3)
最简单的方法可能是使用分析函数。像
这样的东西SELECT *
FROM (
SELECT A.ID AS APP_ID,
A.APPLICATIONSTATUS_ID AS CURR_ST,
Z.ID AS ST_ID,
Z.CREATEDATE,
Z.APPLICATIONSTATUS_ID AS OLD_ST,
rank() over (partition by a.id order by z.createDate desc) rnk
FROM APPLICATION A,
STATUSTRACKING Z
WHERE A.APPLICATIONSTATUS_ID in (5,6)
AND A.ID IN (337,1099,1404,9441)
AND Z.APPLICATIONSTATUS_ID = 7
AND Z.APPLICATION_ID=A.ID
)
WHERE rnk = 1
如果可能存在关联,您可能需要使用ROW_NUMBER
或DENSE_RANK
分析函数而不是RANK
。
答案 1 :(得分:0)
如果我理解正确,您需要该应用的最新状态,对吧?问题:您的创建日期也有时间(小时,分钟和秒)?也许这个查询可以帮助你:
select a.* , st.*
from application a
, statustracking st
where a.id = st.app_id
and st.createdate = ( select max(st1.createdate)
from statustracing st1
where st1.app_id = a.id
and st1.application_statusid = st.application_statusid )
-- filters goes there
and a.id in (....)
and st.application_statusid in (...)