SQL> select * from aaa;
ID SEQ_ID NAME
---------- ---------- --------------------
1 1 A
1 2 A
1 3 A
2 1 B
2 2 B
3 1 C
3 2 C
3 3 C
3 4 C
9 rows selected
SQL>
SQL> SELECT *
2 FROM AAA A
3 WHERE A.SEQ_ID = (SELECT MAX(B.SEQ_ID) FROM AAA B WHERE B.ID = A.ID)
4 /
ID SEQ_ID NAME
---------- ---------- --------------------
1 3 A
2 2 B
3 4 C
www.cnoug.org 论坛 yesl
SELECT ID, SEQ_ID, NAME
FROM (SELECT ID,
SEQ_ID,
NAME,
RANK() OVER(PARTITION BY ID ORDER BY SEQ_ID DESC NULLS LAST) DRN
FROM TA)
WHERE DRN = 1
SQL> SELECT ID, SEQ_ID, NAME
2 FROM (SELECT ID,
3 SEQ_ID,
4 NAME,
5 RANK() OVER(PARTITION BY ID ORDER BY SEQ_ID DESC NULLS LAST) DRN
6 FROM AAA)
7 WHERE DRN =1
8 /
ID SEQ_ID NAME
---------- ---------- --------------------
1 3 A
2 2 B
3 4 C
具体的就不说,下面谈谈这个 Nulls last ,
select v.id,
v.projectyear,
p.oacode,
p.oapublishdate,
dense_rank() OVER(PARTITION BY V.ID, V.projectyear ORDER BY TO_DATE(P.APPLYDATE, 'YYYY-MM-DD HH24:MI:SS') DESC NULLS LAST) DRN
from (select F_GETPROJECTDESIGBINDID(ID) bindid, id, projectyear
from V_GETSUPER) v,
PM_OADATAPROTOCOL_PM p
where v.bindid = p.PMBIZCODE
AND P.PMBIZTYPE = 'DESIGN_DESIGNAUDITING'
AND P.FILETYPE = 'OAFileType0001'
--------------------------------------------------------------------
以上这段代码始终把 null放在最前面。诶;
最后解决方案,在外面再套一层,在里面转date后,再外面order by;
----------------------------------------------------------------------
SELECT ID,projectyear,oacode,oapublishdate,rank() OVER(PARTITION BY ID, projectyear
ORDER BY APPLYDATE DESC NULLS LAST
) DRN,APPLYDATE,APPID FROM (
select v.id,
v.projectyear,
p.oacode,
p.oapublishdate,
TO_DATE(oapublishdate, 'YYYY-MM-DD HH24:MI:SS') APPLYDATE,
P.APPID
from (select F_GETPROJECTDESIGBINDID(ID) bindid, id, projectyear
from V_GETSUPER) v,
PM_OADATAPROTOCOL_PM p
where v.bindid = p.PMBIZCODE
AND P.PMBIZTYPE = 'DESIGN_DESIGNAUDITING'
AND P.FILETYPE = 'OAFileType0001' )
------------------------------
为什么不考虑第一种方案呢,那真是浮云啊。。。。
因为他用了2次表。。。我的结果集都是经过千辛万苦算出来的,我还怎么会舍得让他使用2次啊。。。
---------------下午了,好多地方要用了,看来治标不治本啊;;;;
没办法了,只能出杀手锏了
CREATE OR REPLACE VIEW V_GETLAST_PM AS
SELECT OACODE, OAPUBLISHDATE, PMBIZCODE, PMBIZTYPE, FILETYPE
FROM (SELECT OACODE,
OAPUBLISHDATE,
PMBIZCODE,
PMBIZTYPE,
FILETYPE,
RANK() OVER(PARTITION BY PMBIZCODE ORDER BY APPLYDATE DESC NULLS LAST) DRN
FROM (SELECT PMBIZCODE,
P.OACODE,
P.OAPUBLISHDATE,
TO_DATE(OAPUBLISHDATE, 'YYYY-MM-DD HH24:MI:SS') APPLYDATE,
P.PMBIZTYPE,
P.FILETYPE
FROM PM_OADATAPROTOCOL_PM P))
WHERE DRN = 1