Search code examples
snowflake-cloud-data-platformsql-order-by

Snowflake, How to unload the data in a specific order to the stage (internal/external) in snowflake?


I am able to create internal stage in snowflake and unload the table data to the stage using "COPY INTO" command. The number of records in the table are huge (150K). So when I unload to the stage, there are 3 files created in random order. I want to save the data in ascending or descending order in the stage like we use in order by SQL clause. How can I achieve that?

Example:
stage_0_0_0.csv -> 1 to 50000
stage_0_1_0.csv -> 50001 to 100000
stage_0_2_0.csv -> 100001 to 150001

No need to be exactly the same above but should be in order. Also how can I combine all the three files into one file.

Any help would be highly appreciated.

Thank you all.


Solution

  • When you unload the data into a Stage if you just use COPY INTO @stage from Table; we do not guarantee the order of data.

    However, if you use Order by in the inner select as below, this will order the data as per the column name specified

    COPY INTO @stage FROM (select * from source_table order by column_name) FILE_FORMAT = (TYPE = CSVCOMPRESSION = GZIP FIELD_OPTIONALLY_ENCLOSED_BY = '"') INCLUDE_QUERY_ID = TRUE