[英]How to create select SQL statement that would produce “merged” dataset from two tables(Oracle DBMS)?
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. 所以我有两个表events_source_1和events_source_2表。 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: events_source_1包含历史事件数据,因此我必须获取最新事件(为此,我正在执行以下操作:
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: events_source_2包含将来的事件数据,我必须执行以下操作:
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 (ie when same event_type,b,c found from event_source_2 then next_event_date will be filled with the first date found 如何使用外部连接语句填充空白(即,当从event_source_2找到相同的event_type,b,c时,next_event_date将使用找到的第一个日期填充
GREATLY APPRECIATE FOR YOUR HELP IN ADVANCE. 非常感谢您的帮助。
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
. 这应返回最新
event_date
的events_source_1
每event_type, b, c
,并添加最低event_date
的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
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. 您可以将需要使用group by的max选入一个虚拟表,然后按照我在上一个问题的答案中提供的方式进行完全外部联接。
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. 然后,您可以像过去的实际表一样使用past_source,并在显示的with子句的结束括号后继续选择。
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. 最后,我执行两步过程:第一步填充事件表1中的数据,第二步合并目标(第一步的数据集)和另一个源之间的数据。 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:
这是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)
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.