Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

sql server - SQL copy data to new table adding sequences by data grouping

I am stuck on SQL I need to write (and I'm a SQL novice).

I have a table that contains data with a structure like this:

Table: CURRENT_TABLE

item_id col1 col2 other_col
100 A B C
200 D E F
200 G H I
300 J K L
200 M N O
question from:https://stackoverflow.com/questions/65904695/sql-copy-data-to-new-table-adding-sequences-by-data-grouping

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use row_number():

select item_id,
       row_number() over (partition by item_id order by (select null)) - 1 as seq,
       col1, col2, other_col
from t;

You can use into to put the results in a new table.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...