我有两张表DOCMETA
和REVISIONS
:
CREATE TABLE "DOCMETA"
(
"DID" NUMBER(*,0),
"XIDCPROFILE" VARCHAR2(30 CHAR),
"XDAMENTITY" VARCHAR2(200 CHAR)
---------------------------------------
-- some other columns
---------------------------------------
)
CREATE TABLE "REVISIONS"
(
"DID" NUMBER(*,0),
"DDOCNAME" VARCHAR2(100 BYTE),
"DREVISIONID" NUMBER(*,0),
"DSTATUS" VARCHAR2(20 CHAR)
---------------------------------------
-- some other columns
---------------------------------------
)
现在我想通过将这两个表合并为DOCMETA.DID=REVISIONS.DID
来选择所有列
REVISIONS.DSTATUS
的值不应为EXPIRED
[查询:REVISIONS.DSTATUS NOT IN ('EXPIRED')
]和DOCMETA.XIDCPROFILE
应具有DAMMedia, DAMVideo, DAMFile
[查询:DOCMETA.XIDCPROFILE IN 'DAMMedia','DAMVideo','DAMFile')
]和DOCMETA.XDAMENTITY
不是NULL
且格式为WF/___/DAM
[查询:DOCMETA.XDAMENTITY LIKE 'WF/___/DAM'
]。 以下是REVISIONS
的一部分:
DID DDOCNAME DREVISIONID DSTATUS
-------------------------------------------
25066 MHIS015749 92 RELEASED
25074 MHIS021584 1 RELEASED
25128 MHIS004624 5 RELEASED
25137 MHIS005679 7 RELEASED
25138 MHIS005679 8 RELEASED
我希望获得唯一REVISIONS.DDOCNAME
,REVISIONS.DREVISIONID
应该是最高的。从上面的数据可以看出这两行:
25137 MHIS005679 7 RELEASED
25138 MHIS005679 8 RELEASED
拥有相同的DDOCNAME
所以我想在这两行中DREVISIONID
获得最高行。
我很难通过将所有内容与唯一DDOCNAME
和最高DREVISIONID
的最后两个条件相结合来创建查询。
答案 0 :(得分:0)
您可以使用以下子查询加入RESULT表:
SELECT DID, DDOCNAME, MAX(DREVISIONID) AS LASTREVISIONID
FROM REVISIONS
GROUP BY DID, DDOCNAME
最终查询如下:
SELECT D.DID, R.DDOCNAME, R.REVISIONID, R.DSTATUS
FROM DOCMETA D INNER JOIN
REVISIONS R ON D.DID = R.DID INNER JOIN
(SELECT DID, DDOCNAME, MAX(DREVISIONID) AS LASTREVISIONID
FROM REVISIONS
GROUP BY DID, DDOCNAME) R2 ON R.DID=R2.DID
AND R.DDOCNAME=R2.DDOCNAME
AND r.REVISIONID=R2.LASTREVISIONID
WHERE D.DSTATUS <> 'EXPIRED'
AND XIDCPROFILE IN ('DAMMedia','DAMVideo','DAMFile')
AND XDAMENTITY LIKE 'WF/%/DAM'
答案 1 :(得分:0)
SELECT *
FROM REVISIONS R
INNER JOIN DOCMETA D
ON R.DID=R.DID
AND R.DREVISIONID=
(SELECT MAX(DREVISIONID) FROM REVISIONS b WHERE R.DDOCNAME=b.DDOCNAME
)
您可以将此代码片段与查询的其余部分合并。