views:

60

answers:

3

Here's my original question: merging two data sets

Unfortunately I omitted some intircacies, that I'd like to elaborate here.

So I have two tables events_source_1 and events_source_2 tables. I have to produce the data set from those tables into resultant dataset (that I'd be able to insert into third table, but that's irrelevant).

events_source_1 contain historic event data and I have to do get the most recent event (for such I'm doing the following:

select event_type,b,c,max(event_date),null next_event_date
from events_source_1
group by event_type,b,c,event_date,null

events_source_2 contain the future event data and I have to do the following:

select event_type,b,c,null event_date, next_event_date
from events_source_2
where b>sysdate;

How to put outer join statement to fill the void (i.e. when same event_type,b,c found from event_source_2 then next_event_date will be filled with the first date found

GREATLY APPRECIATE FOR YOUR HELP IN ADVANCE.

+2  A: 

Hope I got your question right. This should return the latest event_date of events_source_1 per event_type, b, c and add the lowest event_date of event_source_2.

Select es1.event_type, es1.b, es1.c,
       Max(es1.event_date),
       Min(es2.event_date) As next_event_date
From events_source_1 es1
Left Join events_source_2 es2 On (     es2.event_type = es1.event_type
                                   And es2.b = es1.b
                                   And es2.c = es1.c
                                 )
Group By c1.event_type, c1.b, c1.c
Peter Lang
A: 

I end up doing two step process: 1st step populates the data from event table 1, 2nd step MERGES the data between target (the dataset from 1st step) and another source. Please forgive me, but I had to obfuscate table name and omit some columns in the code below for legal reasons. Here's the SQL:

    INSERT INTO EVENTS_TARGET (VEHICLE_ID,EVENT_TYPE_ID,CLIENT_ID,EVENT_DATE,CREATED_DATE) 
select VEHICLE_ID, EVENT_TYPE_ID, DEALER_ID, 
max(EVENT_INITIATED_DATE) EVENT_DATE, sysdate CREATED_DATE
FROM events_source_1 
GROUP BY VEHICLE_ID, EVENT_TYPE_ID, DEALER_ID, sysdate;

Here's the second step:

    MERGE INTO EVENTS_TARGET tgt
USING (
  SELECT ee.VEHICLE_ID VEHICLE_ID, ee.POTENTIAL_EVENT_TYPE_ID POTENTIAL_EVENT_TYPE_ID, ee.CLIENT_ID CLIENT_ID,ee.POTENTIAL_EVENT_DATE POTENTIAL_EVENT_DATE FROM EVENTS_SOURCE_2 ee WHERE ee.POTENTIAL_EVENT_DATE>SYSDATE) src
ON (tgt.vehicle_id = src.VEHICLE_ID AND tgt.client_id=src.client_id AND tgt.EVENT_TYPE_ID=src.POTENTIAL_EVENT_TYPE_ID)
WHEN MATCHED THEN
 UPDATE SET tgt.NEXT_EVENT_DATE=src.POTENTIAL_EVENT_DATE
WHEN NOT MATCHED THEN
insert (tgt.VEHICLE_ID,tgt.EVENT_TYPE_ID,tgt.CLIENT_ID,tgt.NEXT_EVENT_DATE,tgt.CREATED_DATE) VALUES (src.VEHICLE_ID, src.POTENTIAL_EVENT_TYPE_ID, src.CLIENT_ID, src.POTENTIAL_EVENT_DATE, SYSDATE)
;
Roman Kagan
+1  A: 

You could just make the table where you need to select a max using a group by into a virtual table, and then do the full outer join as I provided in the answer to the prior question.

Add something like this to the top of the query:

with past_source as (
select event_type, b, c, max(event_date)
from event_source_1
group by event_type, b, c, event_date
)

Then you can use past_source as if it were an actual table, and continue your select right after the closing parens on the with clause shown.

scaling_out
very good comment!!! Greatly appreciate for this tidbit
Roman Kagan