简体   繁体   English

合并两个表而不会丢失列或行

[英]Combining two tables without losing column or rows

I have two tables: 我有两个表:

The first one has the colums "SomeValue" and "Timestamp". 第一个具有列“ SomeValue”和“ Timestamp”。 The other one has the columns "SomeOtherValue" and also "Timestamp". 另一个具有“ SomeOtherValue”列和“ Timestamp”列。

What I need as an output is the following: 我需要的输出如下:

A table with the three Colums "SomeValue", "SomeOtherValue" and "Timestamp". 具有三个列“ SomeValue”,“ SomeOtherValue”和“ Timestamp”的表。

When a row in table 1 is like this: [2; 表1中的行是这样的时: [2; 04/07/2017-20:05] and a row in table 2 is like that: [5; [2017年4月7日至20:05]表2中的一行如下: [5; 04/07/2017-20:05] , I want the combined output row to be [2; 04/07 / 2017-20:05] ,我希望合并的输出行为[2; 5; 5; 04/07/2017-20:05] . 04/07 / 2017-20:05]

Until that point it would be easy done with a simple join, but I also need all other rows. 在此之前,使用简单的连接就可以轻松完成,但是我还需要所有其他行。 So for example if we have a row in table 1 like [2; 例如,如果我们在表1中有一行[2; 04/07/2017-20:05] and no matching timestamp in table 2 , the output should be like [2; 并且表2中没有匹配的时间戳,输出应类似于[2; 04/04 / 2017-20:05] ?; ?; 04/07/2017-20:05] . 04/07 / 2017-20:05] The '?' '?' stands for undefined or null. 代表undefined或null。 It would also be possible to not join two rows with the same timestamp but rather concating both tables, so that every row would have one empty cell with '?'. 也可以不将具有相同时间戳的两行连接在一起,而是将两个表都隐藏起来,以便每一行都将有一个带'?'的空单元格。

I do realize that I didn't use correct Date/Time Format here in that example, but assume that it is used in the database. 我确实意识到在该示例中我没有在此处使用正确的日期/时间格式,但是假定它已在数据库中使用。

I already tried using UNION ALL but it always removes one column. 我已经尝试使用UNION ALL,但是它总是删除一列。 For my use case it is not possible to query both tables independently. 对于我的用例,不可能独立查询两个表。 I really need both values in one row/object. 我确实需要在一行/对象中同时使用两个值。

I hope someone can help me with this. 我希望有人可以帮助我。 Thank you! 谢谢!

What you are describing is a full outer join: 您所描述的是完全外部联接:

select t1.somevalue, t2.someothervalue, timestamp
from t1 
full outer join t2 using (timestamp);

I don't know, however, whether SAP HANA supports the USING clause. 但是,我不知道SAP HANA是否支持USING子句。 Here is the same query with ON instead: 这是使用ON的相同查询:

select 
  t1.somevalue, 
  t2.someothervalue, 
  coalesce(t1.timestamp, t2.timestamp) as timestamp
from t1 
full outer join t2 on t2.timestamp = t1.timestamp;

Joining on a datetime stamp is not always going to be reliable unless you are setting a datetime variable and writing the value of that to both tables. 除非设置日期时间变量并将其值写入两个表,否则联接日期时间戳并不总是可靠的。 It's probably not very efficient either. 这也可能不是很有效。
That said, assuming you want all the results from table 1 and matching table 2 result if it exists then you need a left outer join 也就是说,假设您需要表1的所有结果以及匹配表2的结果(如果存在),则需要左外部联接

Select T1.[SomeValue]
, ISNULL(T2.[SomeOtherValue], '?')
, T1.[TimeStamp]
FROM Table1 T1
LEFT OUTER JOIN Table2 T2
ON T2.[TimeStamp] = T1.[TimeStamp]

Update based on comment from OP If you need all rows from both tables then you could either do 2 queries as above but interchange T1 and T2 position then union the 2 queries. 根据OP的注释进行更新如果您需要两个表中的所有行,则可以如上所述执行2个查询,但互换T1和T2位置,然后合并这2个查询。

SELECT T1.[TimeStamp]
, T1.[SomeValue]
, ISNULL(T2.[SomeOtherValue], '?')
FROM Table1 T1
LEFT OUTER JOIN Table2 T2
ON T2.[TimeStamp] = T2.[TimeStamp]
UNION
SELECT T2.[TimeStamp]
, T2.[SomeValue]
, ISNULL(T1.[SomeOtherValue], '?')
FROM Table2 T2
LEFT OUTER JOIN Table1 T1
ON T1.[TimeStamp] = T2.[TimeStamp]
;

Or you could insert the 1st query results into a table variable then add any missing from T2 rows into that table variable using a where not exists, then select the output. 或者,您可以将第一个查询结果插入到表变量中,然后使用不存在的位置将T2行中的所有缺失项添加到该表变量中,然后选择输出。

DECLARE @TempTab TABLE
( [TimeStamp] [datetime] NOT NULL
, [SomeValue] [nvarchar] (MAX) -- or int if this is always an integer
, [SomeOtherValue] [nvarchar] (MAX) -- or int if this is always an integer
)
;

INSERT INTO @TempTab
( [TimeStamp]
, [SomeValue]
, [SomeOtherValue]
)
SELECT T1.[TimeStamp]
, T1.[SomeValue]
, ISNULL(T2.[SomeOtherValue], '?')
FROM Table1 T1
LEFT OUTER JOIN Table2 T2
ON T2.[TimeStamp] = T2.[TimeStamp]
;

INSERT INTO @TempTab
( [TimeStamp]
, [SomeValue]
, [SomeOtherValue]
)
SELECT T2.[TimeStamp]
, T2.[SomeValue]
, ISNULL(T1.[SomeOtherValue], '?')
FROM Table2 T2
LEFT OUTER JOIN Table1 T1
ON T1.[TimeStamp] = T2.[TimeStamp]
WHERE NOT EXISTS
(   SELECT 1
    FROM @TempTab T
    WHERE T.[TimeStamp] = T2.[TimeStamp]
)
;

SELECT T.[TimeStamp]
, T.[SomeValue]
, T.[SomeOtherValue]
FROM @TempTab T
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM