简体   繁体   中英

Combining two tables without losing column or rows

I have two tables:

The first one has the colums "SomeValue" and "Timestamp". The other one has the columns "SomeOtherValue" and also "Timestamp".

What I need as an output is the following:

A table with the three Colums "SomeValue", "SomeOtherValue" and "Timestamp".

When a row in table 1 is like this: [2; 04/07/2017-20:05] and a row in table 2 is like that: [5; 04/07/2017-20:05] , I want the combined output row to be [2; 5; 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; 04/07/2017-20:05] and no matching timestamp in table 2 , the output should be like [2; ?; 04/07/2017-20:05] . The '?' stands for undefined or 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. 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. Here is the same query with ON instead:

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

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.

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.

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
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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