>  기사  >  데이터 베이스  >  数据库中分组第N条记录获取方式

数据库中分组第N条记录获取方式

WBOY
WBOY원래의
2016-06-07 15:00:181189검색

欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入 数据库中分组第N条记录获取方式 第一条记录Oracle: 1 2 3 select * from ( select row_number() over(partition by num order by num) gid,* from tb) tmp where gid=1; ROW_NUMBER() 说明:返回

欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入

  数据库中分组第N条记录获取方式

  第一条记录Oracle:

1

2

3

<font face="NSimsun">select</font> * from(select row_number() over(partition by num order <font face="NSimsun">by</font> num) gid,* from tb) tmp where <font face="NSimsun">gid=1; </font>

<font face="NSimsun"> </font> 

  ROW_NUMBER()

  说明:返回结果集分区内行的序列号,每个分区的第一行从 1 开始。

  语法:ROW_NUMBER () OVER ( [ ] ) 。

  备注:ORDER BY 子句可确定在特定分区中为行分配唯一 ROW_NUMBER 的顺序。

  参数: :将 FROM 子句生成的结果集划入应用了 ROW_NUMBER 函数的分区。

  :确定将 ROW_NUMBER 值分配给分区中的行的顺序。

  返回类型:bigint 。

  示例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

<font face="NSimsun">/*以下示例将根据年初至今的销售额,返回 AdventureWorks 中销售人员的 ROW_NUMBER。*/</font>

<font face="NSimsun"> </font> 

<font face="NSimsun">USE AdventureWorks </font>

<font face="NSimsun">GO </font>

<font face="NSimsun">SELECT</font> c.FirstName, c.LastName, ROW_NUMBER() OVER(ORDER <font face="NSimsun">BY</font> SalesYTD DESC) AS 'Row Number', s.SalesYTD, a.PostalCode

<font face="NSimsun">FROM</font> Sales.SalesPerson s JOIN Person.Contact c on <font face="NSimsun">s.SalesPersonID = c.ContactID </font>

<font face="NSimsun">JOIN</font> Person.Address a ON <font face="NSimsun">a.AddressID = c.ContactID </font>

<font face="NSimsun">WHERE</font> TerritoryID IS <font face="NSimsun">NOT</font> <font face="NSimsun">NULL</font> <font face="NSimsun">AND</font> <font face="NSimsun">SalesYTD 0 </font>

<font face="NSimsun">/* </font>

<font face="NSimsun">FirstName  LastName    Row Number  SalesYTD      PostalCode </font>

<font face="NSimsun">---------  ----------  ----------  ------------  ---------------------------- </font>

<font face="NSimsun">Shelley    Dyck        1           5200475.2313  98027 </font>

<font face="NSimsun">Gail       Erickson    2           5015682.3752  98055 </font>

<font face="NSimsun">Maciej     Dusza       3           4557045.0459  98027 </font>

<font face="NSimsun">Linda      Ecoffey     4           3857163.6332  98027 </font>

<font face="NSimsun">Mark       Erickson    5           3827950.238   98055 </font>

<font face="NSimsun">Terry      Eminhizer   6           3587378.4257  98055 </font>

<font face="NSimsun">Michael    Emanuel     7           3189356.2465  98055 </font>

<font face="NSimsun">Jauna      Elson       8           3018725.4858  98055 </font>

<font face="NSimsun">Carol      Elliott     9           2811012.7151  98027 </font>

<font face="NSimsun">Janeth     Esteves     10          2241204.0424  98055 </font>

<font face="NSimsun"> </font> 

<font face="NSimsun">Martha     Espinoza    11          1931620.1835  98055 </font>

<font face="NSimsun">Carla      Eldridge    12          1764938.9859  98027 </font>

<font face="NSimsun">Twanna     Evans       13          1758385.926   98055 </font>

<font face="NSimsun">(13 行受影响) </font>

<font face="NSimsun">*/</font>

<font face="NSimsun"> </font> 

<font face="NSimsun">/*以下示例将返回行号为 50 到 60(含)的行,并以 OrderDate 排序。*/</font> 

<font face="NSimsun">USE AdventureWorks; </font>

<font face="NSimsun">GO </font>

<font face="NSimsun">WITH</font> OrderedOrders AS

(SELECT <font face="NSimsun">SalesOrderID, OrderDate, </font>

ROW_NUMBER() OVER (order <font face="NSimsun">by</font> OrderDate)as <font face="NSimsun">RowNumber </font>

<font face="NSimsun">FROM</font> <font face="NSimsun">Sales.SalesOrderHeader )  </font>

<font face="NSimsun">SELECT</font> <font face="NSimsun">*  </font>

<font face="NSimsun">FROM</font> <font face="NSimsun">OrderedOrders  </font>

<font face="NSimsun">WHERE</font> RowNumber between 50 and <font face="NSimsun">60; </font>

<font face="NSimsun">/* </font>

<font face="NSimsun">SalesOrderID OrderDate               RowNumber </font>

<font face="NSimsun">------------ ----------------------- -------------------- </font>

<font face="NSimsun">43708        2001-07-03 00:00:00.000 50 </font>

<font face="NSimsun">43709        2001-07-03 00:00:00.000 51 </font>

<font face="NSimsun">43710        2001-07-03 00:00:00.000 52 </font>

<font face="NSimsun">43711        2001-07-04 00:00:00.000 53 </font>

<font face="NSimsun">43712        2001-07-04 00:00:00.000 54 </font>

<font face="NSimsun">43713        2001-07-05 00:00:00.000 55 </font>

<font face="NSimsun">43714        2001-07-05 00:00:00.000 56 </font>

<font face="NSimsun">43715        2001-07-05 00:00:00.000 57 </font>

<font face="NSimsun">43716        2001-07-05 00:00:00.000 58 </font>

<font face="NSimsun">43717        2001-07-05 00:00:00.000 59 </font>

<font face="NSimsun">43718        2001-07-06 00:00:00.000 60 </font>

<font face="NSimsun">(11 行受影响) </font>

<font face="NSimsun">*/</font>

数据库中分组第N条记录获取方式

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:hadoop2.0日志问题다음 기사:建立2D AABB