Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding row IDs to table in Snowflake SQL

I need to add a column with unique integers/hashes to a table to serve as the row id.

I created a table DUPLICATES with this query:

CREATE TABLE duplicates AS
SELECT
    "a", "b", COUNT(*) as _count
FROM
    "table"
GROUP BY
    "a", "b"
HAVING 
    _count > 1
ORDER BY 
 _count desc

Now I need to add a column that would contain unique ID for each row. I tried ALTER TABLE DUPLICATES ADD 'id' int identity(1,1), however this returns: SQL compilation error: Cannot add column 'id' with non-constant default to non-empty table 'DUPLICATES'.

I am kind of short of ideas now as the discussions on Snowflake SQL are not widespread yet and the help page of Snowflake did not help me.

like image 704
RadRuss Avatar asked Jan 28 '23 05:01

RadRuss


2 Answers

One simple method is to use row_number():

CREATE TABLE duplicates AS
    SELECT ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) as id,
           "a", "b", COUNT(*) as _count
    FROM "table"
    GROUP BY "a", "b"
    HAVING  _count > 1
    ORDER BY _count desc;
like image 148
Gordon Linoff Avatar answered Jan 31 '23 23:01

Gordon Linoff


The easiest way to do this in Snowflake is to use Sequences. Pretty straight forward but they can be used to generate values for a primary key or any column that requires a unique value. How to use:

https://docs.snowflake.net/manuals/user-guide/querying-sequences.html

like image 20
slybitz Avatar answered Feb 01 '23 00:02

slybitz