Select Flashback Query
Description
Oracle
The flashback query claused in Oracle retrieves past data from a table, view, or materialized view. In Oracle, the uses can include:
Restoring deleted data or undoing an incorrect commit, comparing current data with the corresponding data at an earlier time, checking the state of transactional data at a particular time, and reporting generation tools to past data, among others. (Oracle Flashback query documentation).
Snowflake
The equivalent mechanism in Snowflake to query data from the past is the AT | BEGIN
query. Notice that the only equivalent is for the AS OF
statements.
Furthermore, Snowflake has complete "Time Travel" documentation that allows querying data to clone objects such as tables, views, and schemas. There are limitations on the days to access the past or deleted data (90 days before passing to Fail-safe status). For more information, review the Snowflake Time Travel Documentation.
Oracle syntax
{ VERSIONS BETWEEN
{ SCN | TIMESTAMP }
{ expr | MINVALUE } AND { expr | MAXVALUE }
| AS OF { SCN | TIMESTAMP } expr
}
Snowflake Syntax
SELECT ...
FROM ...
{
AT( { TIMESTAMP => <timestamp> | OFFSET => <time_difference> | STATEMENT => <id> | STREAM => '<name>' } ) |
BEFORE( STATEMENT => <id> )
}
[ ... ]
Sample Source Patterns
The following data is used in the following examples to generate the query outputs.
CREATE TABLE Employee (
EmployeeID NUMBER PRIMARY KEY,
FirstName VARCHAR2(50),
LastName VARCHAR2(50),
EmailAddress VARCHAR2(100),
HireDate DATE,
SalaryAmount NUMBER(10, 2)
);
INSERT INTO Employee VALUES (1, 'Bob', 'SampleNameA', '[email protected]', TO_DATE('2023-01-15', 'YYYY-MM-DD'), 11111.00);
INSERT INTO Employee VALUES (2, 'Bob', 'SampleNameB', '[email protected]', TO_DATE('2023-01-15', 'YYYY-MM-DD'), 11111.00);
INSERT INTO Employee VALUES (3, 'Bob', 'SampleNameC', '[email protected]', TO_DATE('2022-03-10', 'YYYY-MM-DD'), 11111.00);
INSERT INTO Employee VALUES (4, 'Bob', 'SampleNameD', '[email protected]', TO_DATE('2022-03-10', 'YYYY-MM-DD'), 11111.00);
INSERT INTO Employee VALUES (5, 'Bob', 'SampleNameE', '[email protected]', TO_DATE('2022-03-10', 'YYYY-MM-DD'), 11111.00);
1. AS OF with TIMESTAMP case
Oracle
SELECT * FROM employees
AS OF TIMESTAMP
TO_TIMESTAMP('2023-09-27 07:00:00', 'YYYY-MM-DD HH:MI:SS')
WHERE last_name = 'SampleName';
Snowflake
SELECT * FROM
employees
!!!RESOLVE EWI!!! /*** SSC-EWI-OR0135 - DATA RETENTION PERIOD MAY PRODUCE NO RESULTS ***/!!!
AT (TIMESTAMP =>
TO_TIMESTAMP('2023-09-27 07:00:00', 'YYYY-MM-DD HH:MI:SS'))
WHERE last_name = 'SampleName';
2. AS OF with SCN case
Oracle
SELECT * FROM employees
AS OF SCN
TO_TIMESTAMP('2023-09-27 07:00:00', 'YYYY-MM-DD HH:MI:SS')
WHERE last_name = 'SampleName';
Snowflake
SELECT * FROM
employees
!!!RESOLVE EWI!!! /*** SSC-EWI-0040 - THE STATEMENT IS NOT SUPPORTED IN SNOWFLAKE ***/!!!
AS OF SCN
TO_TIMESTAMP('2023-09-27 07:00:00', 'YYYY-MM-DD HH:MI:SS')
WHERE last_name = 'SampleName';
Known Issues
1. The option when it is using SCN is not supported.
2. The VERSION statement is not supported in Snowflake.
Related EWIS
SSC-EWI-0040: Statement Not Supported.
SSC-EWI-OR0135: Current of clause is not supported in Snowflake.
SSC-FDM-0006: Number type column may not behave similarly in Snowflake.
Last updated