📔
knowledge
  • Introduction
  • Programming Languages
  • Databases
    • SQL
      • PARTITION BY
  • Me
    • The Software I Use
  • Software
    • Arch Linux
    • NixOS
    • Password Store
    • Vim
    • xmonad
Powered by GitBook
On this page

Was this helpful?

  1. Databases
  2. SQL

PARTITION BY

I can never remember the syntax of these things.

WITH 
    Records AS (
        SELECT 
            col1,
            ROW_NUMBER() OVER (
                PARTITION BY col2 
                ORDER BY col3 DESC
            ) row_num,
          FROM table T
        )

SELECT *
FROM Records
WHERE row_num < 2

This will give you the top 1 record (based on col3) for each group made by col2

PreviousSQLNextMe

Last updated 4 years ago

Was this helpful?