我有以下查詢:甲骨文 - 如何過濾掉重複的行,而無需使用GROUP BY
select
C.PROJECTID, C.VERSIONID, C.MODIFIEDATTRIBUTEID, C.MODIFIEDDATE, V.NAME
from TPM_PROJECTCHANGES C
inner join TPM_PROJECTVERSION V ON C.PROJECTID = V.PROJECTID AND C.VERSIONID = V.VERSIONID
where C.MODIFIEDDATE BETWEEN TO_DATE('07/18/12', 'MM/DD/YY') and TO_DATE('07/25/12', 'MM/DD/YY')
我想表明在某些日期之間的項目的所有更改。這工作正常。但是,我有很多重複項目(例如,如果某人在同一個項目上更改了同一個屬性50次)。我想將它們過濾出來,只顯示項目中某個屬性發生更改的最近時間。我可以做這樣的事情:
select
C.PROJECTID, C.VERSIONID, C.MODIFIEDATTRIBUTEID, MAX(C.MODIFIEDDATE)
from TPM_PROJECTCHANGES C
having MAX(C.MODIFIEDDATE) BETWEEN TO_DATE('07/18/12', 'MM/DD/YY') and TO_DATE('07/25/12', 'MM/DD/YY')
group by C.PROJECTID, C.VERSIONID, C.MODIFIEDATTRIBUTEID
不過,現在我不能在JOIN
什麼。例如,如果我嘗試:
select
C.PROJECTID, C.VERSIONID, C.MODIFIEDATTRIBUTEID, MAX(C.MODIFIEDDATE), V.NAME
from TPM_PROJECTCHANGES C
inner join TPM_PROJECTVERSION V ON C.PROJECTID = V.PROJECTID AND C.VERSIONID = V.VERSIONID
having MAX(C.MODIFIEDDATE) BETWEEN TO_DATE('07/18/12', 'MM/DD/YY') and TO_DATE('07/25/12', 'MM/DD/YY')
group by C.PROJECTID, C.VERSIONID, C.MODIFIEDATTRIBUTEID
我會得到錯誤:
ORA-00979: not a GROUP BY expression
有沒有更好的方法來濾除對同一項目的重複更改?
嘗試在'group_by'之後移動'having' – rogal111 2012-07-25 22:08:13
@ rogal111 - 'HAVING'的位置沒有區別。看起來Oracle(或SQL)不支持這一點,儘管它在邏輯上是有道理的。 – 2012-07-25 22:28:22