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

ROW_NUMBER 查詢

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

問題描述

限時送ChatGPT賬號..

我有一張桌子:

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 彼此相鄰,我希望它是相同的序列,如果不是,則算作一個新行.

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)

他們都不會滿足我想要的條件.有沒有辦法查詢這個?

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 

您想查看停靠點在一行和下一行之間是否發生變化.如果是,您希望增加序列.因此,使用滯后將上一個停靠點放入當前行.

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 是因為它處理 NULL 的方式,而且它比 CASE 更簡潔,但如果您遵循教科書,您可能應該使用 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 子句的分析函數將給出您正在尋找的答案.

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

這篇關于ROW_NUMBER 查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?什么時候不能投射為日期?)
主站蜘蛛池模板: 国产伦精品一区二区三区视频金莲 | 毛片高清| aaa综合国产 | 亚洲精品欧美 | 国产精品久久久乱弄 | 国产特级毛片 | 九九视频在线观看 | 成人福利在线观看 | 亚洲午夜电影 | 综合色播| 欧美久久久久 | 中国美女撒尿txxxxx视频 | 欧美一级在线免费观看 | 成人在线精品视频 | 视频一区在线观看 | 色男人天堂av | 丝袜天堂| 国产成人在线视频 | 久久久精品国产 | 国产一区二区在线免费播放 | 午夜精品一区 | 日韩视频在线免费观看 | 久久精品电影 | 天堂av在线影院 | 涩涩99 | 亚洲精品欧美 | 一区二区三区中文字幕 | 日韩在线观看中文字幕 | www国产成人免费观看视频,深夜成人网 | 午夜精品一区二区三区免费视频 | 成人精品国产 | www.久久国产精品 | 美日韩精品| 精品免费视频 | 伊人久久精品一区二区三区 | 免费一级片 | 综合九九 | 欧美激情久久久 | 国产美女福利在线观看 | 97国产一区二区 | 国产精品中文在线 |