本文介紹了重新編號一組行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個包含數據的表格,如下所示:
I have a table with data which looks something like the following:
# 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.
I would like to be able to delete any of these rows, but somehow get it to renumber the id_within_group.
所以,如果我從上面的 3 個中刪除第二個,它應該自動重新編號然后留給我
SO if I delete the second one from the 3 above, it should automatically renumber then leaving me with
http://www.google.com
google
search engine
1
http://www.bing.com
bing
search engine
2
這可能嗎?
推薦答案
如果您只需要顯示連續整數,我的建議是在 SELECT
中執行此操作.
My suggestion would be to do this in the SELECT
if you just need to display a sequential integer.
SELECT url ,
title ,
[group] ,
id_within_group ,
ROW_NUMBER() over (partition BY [group] ORDER BY id_within_group) AS sequential_index
FROM YourTable
這篇關于重新編號一組行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!