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

ROW_NUMBER 查詢

ROW_NUMBER query(ROW_NUMBER 查詢)
本文介紹了ROW_NUMBER 查詢的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號(hào)..

我有一張桌子:

Trip  Stop  Time 
-----------------
1     A     1:10
1     B     1:16
1     B     1:20
1     B     1:25
1     C     1:31
1     B     1:40
2     A     2:10
2     B     2:17
2     C     2:20
2     B     2:25  

我想在查詢輸出中再添加一列:

I want to add one more column to my query output:

Trip  Stop  Time Sequence
-------------------------
1     A     1:10   1
1     B     1:16   2 
1     B     1:20   2
1     B     1:25   2
1     C     1:31   3
1     B     1:40   4 
2     A     2:10   1
2     B     2:17   2
2     C     2:20   3
2     B     2:25   4  

最難的部分是 B,如果 B 彼此相鄰,我希望它是相同的序列,如果不是,則算作一個(gè)新行.

The hard part is B, if B is next to each other I want it to be the same sequence, if not then count as a new row.

我知道

row_number over (partition by trip order by time)
row_number over (partition by trip, stop order by time)

他們都不會(huì)滿足我想要的條件.有沒有辦法查詢這個(gè)?

None of them will meet the condition I want. Is there a way to query this?

推薦答案

create table test
(trip number
,stp  varchar2(1)
,tm   varchar2(10)
,seq  number);

insert into test values (1,     'A',     '1:10',   1);
insert into test values (1,     'B',     '1:16',   2); 
insert into test values (1,     'B',     '1:20',   2);
insert into test values (1 ,    'B',     '1:25',   2);
insert into test values (1 ,    'C',     '1:31',   3);
insert into test values (1,     'B',     '1:40',   4);
insert into test values (2,     'A',     '2:10',   1);
insert into test values (2,     'B',     '2:17',   2);
insert into test values (2,     'C',     '2:20',   3);
insert into test values (2,     'B',     '2:25',   4);

select t1.*
      ,sum(decode(t1.stp,t1.prev_stp,0,1)) over (partition by trip order by tm) new_seq
from  
     (select t.*
            ,lag(stp) over (order by t.tm) prev_stp
      from   test t
      order  by tm) t1
;

  TRIP S TM                SEQ P    NEW_SEQ
------ - ---------- ---------- - ----------
     1 A 1:10                1            1
     1 B 1:16                2 A          2
     1 B 1:20                2 B          2
     1 B 1:25                2 B          2
     1 C 1:31                3 B          3
     1 B 1:40                4 C          4
     2 A 2:10                1 B          1
     2 B 2:17                2 A          2
     2 C 2:20                3 B          3
     2 B 2:25                4 C          4

 10 rows selected 

您想查看??奎c(diǎn)在一行和下一行之間是否發(fā)生變化.如果是,您希望增加序列.因此,使用滯后將上一個(gè)??奎c(diǎn)放入當(dāng)前行.

You want to see if the stop changes between one row and the next. If it does, you want to increment the sequence. So use lag to get the previous stop into the current row.

我使用 DECODE 是因?yàn)樗幚?NULL 的方式,而且它比 CASE 更簡(jiǎn)潔,但如果您遵循教科書,您可能應(yīng)該使用 CASE.

I used DECODE because of the way it handles NULLs and it is more concise than CASE, but if you are following the text book, you should probably use CASE.

使用 SUM 作為帶有 ORDER BY 子句的分析函數(shù)將給出您正在尋找的答案.

Using SUM as an analytic function with an ORDER BY clause will give the answer you are looking for.

這篇關(guān)于ROW_NUMBER 查詢的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應(yīng)該使用什么 SQL Server 數(shù)據(jù)類型來存儲(chǔ)字節(jié) [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm Does not return all data(Typeorm 不返回所有數(shù)據(jù))
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轉(zhuǎn)換為“2016-07-01 13:12:22小時(shí)格式?)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應(yīng)該返回“1?什么時(shí)候不能投射為日期?)
主站蜘蛛池模板: 免费h视频 | 日韩久久久久 | 国产一区二区不卡 | 人人擦人人 | 亚洲一二三视频 | 欧美国产日韩在线观看成人 | 99久久精品一区二区成人 | 国产三区精品 | 欧美黄视频 | 久久久黄色 | 免费同性女女aaa免费网站 | 国产福利在线 | 精品国产91亚洲一区二区三区www | 久久久久国产一区二区三区 | 国产视频精品视频 | 91五月天 | 久久久久久久一区 | 久久国产激情视频 | 最新中文字幕在线 | 中国一级毛片免费 | 97伦理电影 | 国产精品视频yy9299一区 | 色婷婷一区二区三区四区 | 中文字幕国产 | 国产精品高| 免费观看一区二区三区毛片 | 成人免费一区二区三区视频网站 | 在线免费观看毛片 | 91精品入口蜜桃 | 午夜在线小视频 | 精品一二三区视频 | 久久久国产精品视频 | 精品伊人 | 国产精品国产成人国产三级 | 国产欧美精品一区二区三区 | 国产成人综合在线 | 精品一二三区 | 日日爱av| 久久国产精品一区二区三区 | 欧美激情一区 | 久草中文在线观看 |