{"id":881,"date":"2023-05-29T20:53:38","date_gmt":"2023-05-29T20:53:38","guid":{"rendered":"https:\/\/bandittracker.com\/?p=881"},"modified":"2023-05-31T19:20:33","modified_gmt":"2023-05-31T19:20:33","slug":"snowflake-distinct-on","status":"publish","type":"post","link":"https:\/\/bandittracker.com\/snowflake-distinct-on\/","title":{"rendered":"How To Use DISTINCT ON With Snowflake"},"content":{"rendered":"\n

PostgreSQL’s DISTINCT ON clause is a powerful tool for filtering result sets. However, the syntax is not available in Snowflake. Thankfully, there are several methods you can use to get the same results.<\/p>\n\n\n\n

This article walks you through three techniques that provide the equivalent of DISTINCT ON in Snowflake.  Each method combines the ROW_NUMBER() windows function with additional syntax.<\/p>\n\n\n\n

The first two methods use standard SQL syntax. The third method is non-standard SQL specific to Snowflake and some other database platforms.<\/p>\n\n\n\n

    \n
  1. Use ROW_NUMBER() with a CTE<\/li>\n\n\n\n
  2. Use ROW_NUMBER() with a subquery<\/li>\n\n\n\n
  3. Use ROW_NUMBER() with the QUALIFY clause<\/li>\n<\/ol>\n\n\n\n
    \n

    Table of Contents<\/p>\n