Auto-increment field that resets after a change in another field

前端 未结 2 1906
太阳男子
太阳男子 2021-01-16 07:31

Can you provide a very simple SQL example of how to create a \"count\" or \"order\" field that would auto-increment, but restart after every change in a different field? In

2条回答
  •  萌比男神i
    2021-01-16 08:14

    Instead of storing Order in the table, consider adding it to a view. You can select from the view instead of the table when you need it.

    The view could use row_number() to calculate the order, like:

    select  row_number() over (partition by Meal order by Time)
    ,       *
    from    YourTable
    

    Example at SE Data.

提交回复
热议问题