Search code examples
sqlauto-incrementfoxpro

How to add auto increment id according to a group in VFP 9 and SQL Server


Here is the format of the table:

No      group   name

1       abc     a
1       xyz     c
1       xyz     d

Now i want it to be like,

No      group   name

1       abc     a
2       abc     b
3       abc     f
1       xyz     c
2       xyz     h
1       xyz     d

"No" should auto increment according to "group" Thank you


Solution

  • You can use windowing function:

    Select group, 
    name, 
    row_number() over (partition by group order by name) as No
    from table