0
votes

(Submitting on behalf of a Snowflake User)


We have wrong duplicate id loaded in the table and we need to correct it. The rules to update the id is whenever there is a time difference of more than 30 min, the id should be new/unique. I have written the query to filter that out, however update is not happening

The below query is there to find the ids to be updated. For testing I have used a particular id.

select id,

BEFORE_TIME,

TIMESTAMP,

datediff(minute,BEFORE_TIME,TIMESTAMP) time_diff,

row_number() over (PARTITION BY id ORDER BY TIMESTAMP) rowno,

concat(id,to_varchar(rowno)) newid from

(SELECT id,

TIMESTAMP,

LAG(TIMESTAMP_EST) OVER (PARTITION BY visit_id ORDER BY TIMESTAMP) as BEFORE_TIME

FROM table_name t

where id = 'XX1X2375'

order by TIMESTAMP_EST)

where BEFORE_TIME is not NULL and time_diff > 30

order by time_diff desc

;

And i could see the 12 record with same id and time difference more than 30. However when I am trying to update. the query is succesfull but nothing is getting update.

update table_name t

set t.id = c.newid

from

(select id ,

BEFORE_TIME,

TIMESTAMP,

datediff(minute,BEFORE_TIME,TIMESTAMP) time_diff,

row_number() over (PARTITION BY id ORDER BY TIMESTAMP) rowno,

concat(id,to_varchar(rowno)) newid from

(SELECT id,

TIMESTAMP,

LAG(TIMESTAMP) OVER (PARTITION BY visit_id ORDER BY TIMESTAMP) as BEFORE_TIME

FROM table_name t

where id = 'XX1X2375'

order by TIMESTAMP_EST)

where BEFORE_TIME is not NULL and time_diff > 30

order by time_diff desc) c

where t.id = c.id

and t.timestamp = c.BEFORE_TIME

;

please note:

I even created a temp table t1 from the above subquery.

And i can see the records in table t1.

when doing select with join with main table i can even see in record in main table.

But again when I am trying to update using new t1. its just showing zero record updated.

I even tried merge but same issue.

MERGE INTO snowplow_data_subset_temp t

USING t1

ON (trim(t.visit_id) = trim(t1.visit_id) and trim(t1.BEFORE_DATE) = trim(t.TIMESTAMP_EST))

WHEN MATCHED THEN UPDATE SET visit_number = newid;

Any recommendations, ideas, or work-arounds? Thanks!

1

1 Answers

0
votes

This looks like they may be running into two things: The table that you created t1, was it a transient or cloned table? Check out the

Get_DDL('t1', 'schemaname');

to check if there are any constraints on the temp table in the session you work on this next. Or you can query the table constraints view "Alternatively, retrieve a list of all table constraints by schema (or across all schemas in a database) by querying the TABLE_CONSTRAINTS View view in the Information Schema." from: https://docs.snowflake.net/manuals/user-guide/table-considerations.html#referential-integrity-constraints

Since the sub query is working just fine - the merge and update statements are clues for what to look for, this is what I found in the documentation for more general info:

*Limitations on Sub queries: https://docs.snowflake.net/manuals/user-guide/querying-subqueries.html#limitations

You can also check to see if there are any errors for the update query by altering the session: https://docs.snowflake.net/manuals/sql-reference/sql/update.html#usage-notes

ALTER SESSION SET ERROR_ON_NONDETERMINISTIC_UPDATE=TRUE;

Here is an example of how to use an update with a Temp table: https://snowflakecommunity.force.com/s/question/0D50Z00008P7BznSAF/can-you-use-a-cte-or-temp-table-with-an-update-statement-to-update-a-table

I am looking forward to seeing how they ended up solving the issue.