0

I have table_a with column product_id, store_id, x.

I want to

create table_b as
select store_id, product_id, sequence_number
from table_a

sequence_number is auto generated number that is supposed to look like this:

store_id            |   product_id              |   sequence_number
1                   |   1                       |   1
1                   |   1                       |   2
1                   |   1                       |   3
1                   |   2                       |   1
1                   |   2                       |   2
2                   |   1                       |   1
2                   |   1                       |   2

Is it possible to do this with sql query?

1

1 Answer 1

1

You can do this with row_number():

create table_b as
    select store_id, product_id,
           row_number() over (partition by store_id, product_id
                              order by NULL
                             ) as sequence_number
    from table_a;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! But what's the meaning of order by NULL?
@YukaFujisaku . . . It is equivalent to leaving out the order by clause. However, I think leaving it out is a bit confusing, because most databases require it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.