Reviewing the Old Sql Server2008 series 06: inquiry - paging and sorting query

Speaking of paging would have to mention the limit Mysql, and feel than some of sql server to be easy; if you want to achieve Limit in sql server2008 in, you can use a subquery;

The paging sql server (assuming that argument) keyword is top, usually used in conjunction order by (sorting); if you do not sort, query may not get the results you want.

Let me talk about the positive sequence with the decommitment

Such as:

. 1  - query male recruits eid, hida; sorted in order hida 
2  SELECT EID, hida 
 . 3      from EMP 
 . 4      WHERE In Flag = . 1  and Sex = ' M ' 
. 5      Order  by hida 
 . 6  - reverse 
. 7  SELECT EID, hida
 . 8      from EMP
 . 9      WHERE In Flag = . 1  and Sex = ' M ' 
10      Order  by Hida desc

 

 ASC is the order, the default may be omitted, is desc reverse (not shown), directly added to the end of each sort field.

So If you want to query the first three, how to do it. When queried directly coupled with top on it;

Such as:

. 1  - query male recruits eid, hida; hida prior to the sort order three top (Expression) 
2  SELECT  Top ( . 3 ) EID, hida 
 . 3      from EMP 
 . 4      WHERE In Flag = . 1  and Sex = ' M ' 
. 5      Order  by Hida 
 . 6  - before the reverse 3 
. 7  SELECT  Top ( . 4 - . 1 ) EID, Hida
 . 8      from EMP
 . 9      WHERE In Flag = . 1  and Sex = ' M' 
10      order  by Hida desc

 

Note: the expression can be added behind the top (column may be calculated)

What if we need to order the first two records, or in the order of 2-4 three rows of it?

The series will need 07 (subquery) helped

 

Guess you like

Origin www.cnblogs.com/azrealer/p/11857467.html