17.10 - Examples: MERGE Target PTI Time-Bucket Table with Source PTI Time-Bucket Table - Advanced SQL Engine - Teradata Database

Teradata Vantageā„¢ - Time Series Tables and Operations

Product
Advanced SQL Engine
Teradata Database
Release Number
17.10
Release Date
July 2021
Content Type
Programming Reference
Publication ID
B035-1208-171K
Language
English (United States)

This example merges the source sequenced PTI table src_s into the target sequenced PTI table tgt_s.

MERGE INTO
tgt_s AS tgt
USING src_s AS src
ON tgt.TD_TIMECODE = src.TD_TIMECODE AND
   tgt.TD_SEQNO = src.TD_SEQNO AND
   tgt.c1 = src.c1
WHEN MATCHED THEN UPDATE SET c2 = 70
WHEN NOT MATCHED THEN INSERT
(src.TD_TIMECODE, src.TD_SEQNO, src.c1, src.c2);

This example uses a subquery to merge the source sequenced PTI table src_s into the target sequenced PTI table tgt_s.

MERGE INTO
tgt_s AS tgt
USING (SELECT TD_TIMECODE, TD_SEQNO, c1, c2 FROM src_s) AS src
ON tgt.TD_TIMECODE = src.TD_TIMECODE AND
   tgt.TD_SEQNO = src.TD_SEQNO AND
   tgt.c1 = src.c1
WHEN MATCHED
THEN UPDATE SET c2 = 70
WHEN NOT MATCHED THEN
INSERT (src.TD_TIMECODE,src.TD_SEQNO, src.c1, src.c2);

This example merges the source sequenced PTI table src_s into the target sequenced PTI table tgt_s, using INSERT VALUES for the WHEN NOT MATCHED condition.

MERGE INTO
tgt_s AS tgt
USING src_s AS src
ON tgt.TD_TIMECODE = src.TD_TIMECODE AND
   tgt.TD_SEQNO = src.TD_SEQNO AND
   tgt.c1 = src.c1
WHEN MATCHED THEN UPDATE SET c2 = 70
WHEN NOT MATCHED THEN INSERT (TD_TIMECODE, TD_SEQNO, C1, C2)
VALUES (src.TD_TIMECODE, src.TD_SEQNO, src.c1, src.c2);

This merge attempt results in an error because the source sequenced PTI table src_s time bucket duration of HOURS(1) does not match the target sequenced PTI table tgt_s2 time bucket duration of HOURS(2).

MERGE INTO
tgt_s2 AS tgt
USING src_s AS src
ON tgt.TD_TIMECODE = src.TD_TIMECODE AND
   tgt.TD_SEQNO = src.TD_SEQNO
WHEN MATCHED THEN UPDATE SET c2 = 70
WHEN NOT MATCHED THEN INSERT
(src.TD_TIMECODE, src.TD_SEQNO, src.c1, src.c2);