I have below data table where I wants to create a unique id for data_id which will be unique per group_id (group_id and data_id should be unique) and for each group data_id should start from 1. How to create a DB sequence to achieve this ? Or any other better approach to achieve this ?
group_id data_id
UUID-1 1
UUID-1 2
UUID-1 3
UUID-1 4
UUID-2 1
UUID-2 2
row_number()data_idalready seems to meet your requirements. You also define what you want a both a "unique id" and "unique id per group". Something either is or is not unique, so that is confusing.group_id. Is there a specific reason why you need your keys to be like this?