|Group By: A much faster alternative [message #370952]
||Mon, 13 March 2000 05:23
Registered: March 2000
I have an ITEM table which might have several revisions for each ITEM.
ITEM_KEY REVISION ITEM_NAME PRODUCT_TYPE
10000 V-0001 El. Card C
10000 V-0002 El. Card B
20000 V-0001 Cable D
20000 V-0002 Cable A
20000 V-0003 Cable B
I only want information for the latest revision.
SELECT ITEM_KEY, max(REVISION)
FROM ITEM group by ITEM_KEY
I also want to show other information, like PRODUCT_TYPE.
How Do I get the corresponding PRODUCT_TYPE for the selected REVISION.
I can't use max(PRODUCT_TYPE). It will return A for ITEM 20000.
I now that the max REVISION is always the last record/line for an ITEM.
So doing this in MSACCESS I just use the Aggregate Operator LAST.
I haven't found an equivalent operator in Oracle.
Solution that works:
FROM ITEM A
WHERE A.REVISION = (SELECT max(B.REVISION)
FROM ITEM B
WHERE B.ITEM_KEY = A.ITEM_KEY
group by B.ITEM_KEY );
The very fast alternative without using Group by:
> SELECT MA.ITEM_KEY,MA.ITEM_NAME FROM ITEM MA
> WHERE REVISION=(SELECT MAX(REVISION) FROM ITEM SL WHERE SL.ITEM_KEY=MA.ITEM_KEY)
Uses less than 1 second.
The other alternatives with IN and Group By, uses approx. 18 seconds.