Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can number rows in SQL according to the keyed group in sequence by using the ROW_NUMBER() function. Here's an example:

SELECT key, value, ROW_NUMBER() OVER (PARTITION BY key ORDER BY value) as row_number
FROM my_table
ORDER BY key, value;

In this example, we're selecting the key and value columns from a table called "mytable". We're also using the ROWNUMBER() function to number the rows within each group of keys. The PARTITION BY clause specifies that we want to partition the rows by their key values, and the ORDER BY clause specifies that we want to order the rows within each partition by their value. Finally, we're ordering the results by key and value to make it easier to see the sequence of row numbers for each key.