简体   繁体   English

如何创建从两个表(Oracle DBMS)生成“合并的”数据集的选择SQL语句?

[英]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_dateevents_source_1event_type, b, c ,并添加最低event_dateevent_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.

相关问题 如何使用select SQL语句(Oracle DBMS)产生两个数据集的“合并”? - How to produce “merge” of two data sets with select SQL statement (Oracle DBMS)? Oracle SQL如何从select语句创建更新脚本 - Oracle SQL How to create an update script from a select statement 我将如何从两个不同的表中进行选择并在 Oracle 中获得以下内容 - How would I select from two different tables and get the following in Oracle Oracle Select语句从两个表计算计数 - Oracle Select statement calculating count from two tables Oracle PL / SQL。 DBMS_UTILITY.EXEC_DDL_STATEMENT和DBMS_ADVISOR.create_task - Oracle PL/SQL. DBMS_UTILITY.EXEC_DDL_STATEMENT and DBMS_ADVISOR.create_task SQL,Oracle,使用where语句从两个表更新 - SQL, Oracle, Updating from two tables using a where statement INSERT SELECT sql 语句 - 如何在一个语句中从另外两个表中获取数据 - INSERT SELECT sql statement - how to fetch data from two other tables in one statement 如何在Oracle 11g中使用SQL对象关系语句从两个表中提取数据 - How to extract data from two tables using SQL object relational statement in oracle 11g 使用DBMS_PASSTHROUGH从Oracle创建SQL Server别名 - Using DBMS_PASSTHROUGH to create SQL Server aliases from Oracle 如何从SELECT在Oracle中的SQL中使用两个表的列中求和 - How to add up summed values from a column where SELECT used two tables in SQL in Oracle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM