SELECT MAX from UNION Query

Hi,

I am joining 3 queries with a UNION and when I get the results I want to only get one row for each lot determined by the MAX(PDATETIME). I can not group by the other columns as they differ and I can also not MAX these to get past this.This SQL will in turn be used by a different program which can not use analytical functions so rank or row_number are out the window too.
I have that nagging feeling that Im forgetting something very basic to fix this. Can you let me know what you think please?



SELECT    ALUM_HIST.PDATETIME,
    ALUM_HIST.LOT,
    ALUM_HIST.STAGE,
    ALUM_HIST.STEP,
    ALUM_HIST.MACH,
    ALUM_HIST.RUNNUM,
    ALUM_HIST.PASSFAIL
FROM
    WAFER.ALUM_HIST ALUM_HIST
WHERE
    ALUM_HIST.PDATETIME >= SYSDATE -4 AND ALUM_HIST.MACH LIKE 'CPT%'
UNION
SELECT
        ALD_HIST.PDATETIME, 
    ALD_HIST.LOT, 
    ALD_HIST.STAGE, 
    ALD_HIST.STEP, 
    ALD_HIST.MACH, 
    ALD_HIST.RUNNUM,
    ALD_HIST.PASSFAIL
FROM
    WAFER.ALD_HIST ALD_HIST
WHERE
    ALD_HIST.PDATETIME >= SYSDATE -4
UNION
SELECT
        GAPDEP_HIST.PDATETIME, 
    GAPDEP_HIST.LOT, 
    GAPDEP_HIST.STAGE, 
    GAPDEP_HIST.STEP, 
    GAPDEP_HIST.MACH, 
    GAPDEP_HIST.RUNNUM, 
    GAPDEP_HIST.PASSFAIL
FROM
    WAFER.GAPDEP_HIST GAPDEP_HIST
WHERE
    (GAPDEP_HIST.MACH LIKE '602%' OR GAPDEP_HIST.MACH LIKE 'COR%') AND GAPDEP_HIST.PDATETIME >= SYSDATE -4
ORDER BY LOT

Open in new window

GerardMcLAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

scorneoCommented:
Do this solves ur query ?
WITH TEMP AS
(SELECT  ALUM_HIST.PDATETIME,
    ALUM_HIST.LOT,
    ALUM_HIST.STAGE,
    ALUM_HIST.STEP,
    ALUM_HIST.MACH,
    ALUM_HIST.RUNNUM,
    ALUM_HIST.PASSFAIL
FROM
    WAFER.ALUM_HIST ALUM_HIST
WHERE
    ALUM_HIST.PDATETIME >= SYSDATE -4 AND ALUM_HIST.MACH LIKE 'CPT%'
UNION
SELECT
        ALD_HIST.PDATETIME, 
    ALD_HIST.LOT, 
    ALD_HIST.STAGE, 
    ALD_HIST.STEP, 
    ALD_HIST.MACH, 
    ALD_HIST.RUNNUM,
    ALD_HIST.PASSFAIL
FROM
    WAFER.ALD_HIST ALD_HIST
WHERE
    ALD_HIST.PDATETIME >= SYSDATE -4
UNION
SELECT
        GAPDEP_HIST.PDATETIME, 
    GAPDEP_HIST.LOT, 
    GAPDEP_HIST.STAGE, 
    GAPDEP_HIST.STEP, 
    GAPDEP_HIST.MACH, 
    GAPDEP_HIST.RUNNUM, 
    GAPDEP_HIST.PASSFAIL
FROM
    WAFER.GAPDEP_HIST GAPDEP_HIST
WHERE
    (GAPDEP_HIST.MACH LIKE '602%' OR GAPDEP_HIST.MACH LIKE 'COR%') AND GAPDEP_HIST.PDATETIME >= SYSDATE -4
ORDER BY LOT )

SELECT 
    MAXDATE,
    LOT,
    STAGE,
    STEP,
    MACH,
    RUNNUM,
    PASSFAIL
FROM TEMP,
(SELECT MAX(PDATETIME) MAXDATE FROM TEMP GROUP BY PdateTIME) TEMP2
WHERE TEMP.LOT = TEMP2.LOT

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GerardMcLAuthor Commented:
This does thank you.
I was also wondering how I could adapt this to run as a stand alone SQL query
0
scorneoCommented:
This will work as a stand alone sql query.
Do u have any problem at it ?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.