久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

ROW_Count() 根據順序重新開始

ROW_Count() To Start Over Based On Order(ROW_Count() 根據順序重新開始)
本文介紹了ROW_Count() 根據順序重新開始的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..
Create Table #Test (
    ID Int Primary Key Identity,
    Category VarChar(100)
    )

Insert into #Test
(Category)
Values
('Banana'),
('Banana'),
('Banana'),
('Banana'),
('Banana'),
('Banana'),
('Strawberry'),
('Strawberry'),
('Strawberry'),
('Banana'),
('Banana')

Select
     *
    ,ROW_NUMBER() Over (Partition by Category order by ID) as RowNum

From #Test

Order by ID

所以這個腳本返回這個:

So this script returns this:

ID  Category    RowNum
1   Banana      1
2   Banana      2
3   Banana      3
4   Banana      4
5   Banana      5
6   Banana      6
7   Strawberry  1
8   Strawberry  2
9   Strawberry  3
10  Banana      7
11  Banana      8

這是完全有道理的,除了我希望它返回這個:

Which makes perfect sense, except I want it to return this:

ID  Category    RowNum
1   Banana      1
2   Banana      2
3   Banana      3
4   Banana      4
5   Banana      5
6   Banana      6
7   Strawberry  1
8   Strawberry  2
9   Strawberry  3
10  Banana      1
11  Banana      2

我希望它在遇到一組新香蕉時重新開始計數.顯然,我的數據并不是真正的香蕉,但它使可視化變得容易.

I want it to restart the count when it hits a new set of Banana. Obviously my data is not really bananas, but it makes it easy to visualize.

香蕉的這種復發被認為是新的,所以當我們看到這一點時,我們想從一個開始數.我一直在絞盡腦汁,想不出一個好的方法來做到這一點.我明白為什么它不工作,但想不出辦法讓它工作.關于做到這一點的最佳方法有什么建議嗎?

This recurrence of bananas is considered to be new, so we want to start counting from one when we see this. I've been racking my brain and can't think of a good way to do this. I understand why it is not working but can't think of a way to make it work. Any advice on the best way to do this?

推薦答案

有幾種不同的方法可以解決這個問題.一種方法是 row_number() 方法的不同.此方法將識別相同的相鄰類別組:

There are several different ways to approach this. One method is the difference of row_number() approach. This method will identify groups of adjacent categories that are the same:

Select t.*,
       row_number() over (partition by grp, category order by id) as rownum
From (select t.*, 
             (row_number() over (order by id) -
              row_number() over (partition by category order by id)
             ) as grp
      from #Test t
     ) t
Order by ID;

您也可以使用 lag() 計算分組,但這將適用于 SQL Server 2005 和 2008 以及更新的版本.

You can also figure out the groupings using lag(), but this will work in SQL Server 2005 and 2008 as well as more recent versions.

這篇關于ROW_Count() 根據順序重新開始的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm Does not return all data(Typeorm 不返回所有數據)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
How to convert #39;2016-07-01 01:12:22 PM#39; to #39;2016-07-01 13:12:22#39; hour format?(如何將“2016-07-01 01:12:22 PM轉換為“2016-07-01 13:12:22小時格式?)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
主站蜘蛛池模板: 成人av在线播放 | 91丨九色丨国产在线 | 亚洲黄色网址视频 | 久久精品毛片 | 91社区在线观看播放 | 91精品综合久久久久久五月天 | 国产分类视频 | 午夜视频一区 | 国产精品欧美一区二区三区不卡 | www.婷婷亚洲基地 | 欧美日韩国产高清 | 国户精品久久久久久久久久久不卡 | 国产精品99久久久精品免费观看 | 日韩免费高清视频 | 国产精品高清一区二区 | 99精品久久久久 | 久草.com| 久久久久91 | 日本免费在线看 | 亚洲人成人一区二区在线观看 | 毛片在线看看 | 国产黄色网址在线观看 | 中文字幕人成人 | 亚洲另类春色偷拍在线观看 | 中文字幕一区二区三区乱码在线 | 成人在线小视频 | 中文字幕免费在线观看 | 国产高清免费视频 | 久久一区二区三区免费 | 国产精品一区二区久久久久 | 一区日韩 | 狠狠干综合视频 | 日韩一区二区三区在线看 | 中文字幕在线观看一区二区 | 九九久久久久久 | 国产真实乱对白精彩久久小说 | 欧美精品久久久久 | 鸳鸯谱在线观看高清 | 国产精品毛片一区二区在线看 | 伊人艹 | 精品久久国产 |