Search code examples
sqlt-sqldatesequential

Add Column To List Records Sequentially By Date


I have a table with a bunch of dates in random order. How could I add a column that would sequentially number the dates in descending order.

ORIGINAL TABLE

ID   DATE
1    10/08/15
2    10/06/15
3    10/11/15
4    10/07/15

MODIFIED TABLE

ID   DATE       ORDER
1    10/08/15   2
2    10/06/15   4
3    10/11/15   1
4    10/07/15   3

Solution

  • With window functions:

    select *, row_number() over(order by date desc) as [order] from tablename