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

如何在選擇中增加計(jì)數(shù)器

How to increase counter in select(如何在選擇中增加計(jì)數(shù)器)
本文介紹了如何在選擇中增加計(jì)數(shù)器的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有這種情況-

Column A
    1
    0
    0
    0
    1
    0
    0
    0
    0
    1
    0
    1
    0
    0 
    1
    0

我想要這樣的東西-

Column A                      Column B
    1                         1
    0                         1
    0                         1
    0                         1
    1                         2
    0                         2
    0                         2
    0                         2
    0                         2
    1                         3
    0                         3
    1                         4
    0                         4
    0                         4
    1                         5
    0                         5

就像在 A 列中每次出現(xiàn) 1 一樣,我們將 B 列中的數(shù)字增加一.我想在一個(gè)選擇中有這個(gè).我不能為此使用循環(huán).

Its like for each occurance of 1 in column A we are increasing the number in column B by one. I want to have this in a select. I can't use loop for this.

我使用的是 SQL-Server 2008 R2.任何人都可以請(qǐng)告訴我它是如何做到的.提前致謝.

I am using SQL-Server 2008 R2. Can anyone please give me idea how it can done. Thanks in advance.

推薦答案

使用 cte 和窗口函數(shù) Row_Number()... 但是,我要注意,最好在 OVER 子句中替換 (Select NULL)具有適當(dāng)?shù)捻樞?即身份 int、日期時(shí)間).

With a cte and window function Row_Number()... However, I should note that it would be best if you replace (Select NULL) in the OVER clause with a proper sequence (ie identity int, datetime).

Declare @YourTable table (ColumnA int)
Insert Into @YourTable values (1),(0),(0),(0),(1),(0),(0),(0),(0),(1),(0),(1),(0),(0),(1),(0)

;with cte as (
    Select *,RN=Row_Number() over (Order By (Select Null)) from @YourTable
)
Select A.ColumnA
      ,ColumnB = sum(B.ColumnA)
 From  cte A
 Join  cte B on (B.RN<=A.RN)
 Group By A.ColumnA,A.RN
 Order By A.RN

退貨

ColumnA ColumnB
1       1
0       1
0       1
0       1
1       2
0       2
0       2
0       2
0       2
1       3
0       3    
1       4
0       4
0       4
1       5
0       5

這篇關(guān)于如何在選擇中增加計(jì)數(shù)器的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Modify Existing decimal places info(修改現(xiàn)有小數(shù)位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關(guān)名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號(hào)或管道運(yùn)算符字符串中刪除重復(fù)項(xiàng))
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關(guān)系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 麻豆a级片 | 国产精品伦一区二区三级视频 | av在线一区二区三区 | 成人h视频在线 | 亚洲国产一区二区三区在线观看 | 亚洲人人舔人人 | 国产99久久 | 久久久精品一区二区 | 亚洲精品中文字幕中文字幕 | 最新国产福利在线 | 亚洲视频精品在线 | 第一区在线观看免费国语入口 | 91久久久久久 | 91国产精品 | 精品视频在线免费观看 | 99国产视频 | 国产在线精品免费 | 午夜成人免费视频 | 免费视频一区二区 | av在线播放网 | 亚洲最色网站 | 97成人在线 | 日韩精品久久久久 | 国产美女视频黄a视频免费 国产精品福利视频 | 精品国产一区二区三区免费 | 精品国产区 | www.久久久| 国产精品久久久久久久久久免费看 | 九色视频网站 | 国产高清在线观看 | 亚洲视频在线播放 | 欧美精品久久久 | 国产91丝袜在线18 | 欧美亚洲激情 | 九九热视频这里只有精品 | 九九九精品视频 | 亚洲精品国产a久久久久久 中文字幕一区二区三区四区五区 | 久久亚洲一区二区三区四区 | 无码一区二区三区视频 | 久久av一区二区三区 | 日韩字幕 |