17.00 - Examples: MERGE Target PTI Time-Bucket Table with Source PTI Time-Bucket Table - Teradata Database

Teradata Vantageā„¢ - Time Series Tables and Operations

prodname
Advanced SQL Engine
Teradata Database
vrm_release
17.00
created_date
June 2020
category
Programming Reference
featnum
B035-1208-170K

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);