我有一个包含数据的表格,如下所示:
# the columns #
url
title
group
id_within_group
http://www.google.com
google
search engine
1
http://www.yahoo.com
yahoo
search engine
2
http://www.bing.com
bing
search engine
3
http://www.facebook.com
facebook
social media
1
http://www.twitter.com
twitter
social media
2
如果我这样做
select * from table1 where group = "search engine"
我会得到
http://www.google.com
google
search engine
1
http://www.yahoo.com
yahoo
search engine
2
http://www.bing.com
bing
search engine
3
我希望能够删除任何这些行,但不知何故让它重新编号id_within_group。
所以,如果我删除上面3中的第二个,它应该自动重新编号,然后留给我
http://www.google.com
google
search engine
1
http://www.bing.com
bing
search engine
2
这可能吗?
答案 0 :(得分:6)
如果您只需要显示一个连续的整数,我的建议是在SELECT
中执行此操作。
SELECT url ,
title ,
[group] ,
id_within_group ,
ROW_NUMBER() over (partition BY [group] ORDER BY id_within_group) AS sequential_index
FROM YourTable