The ROW_NUMBER () function uses Detailed

Original Address: https://blog.csdn.net/qq_30908543/article/details/74108348

Note: mysql looks like NA, I tested unsuccessful, mysql implementations may refer to: https://www.cnblogs.com/phpk/p/10935655.html

Examples are as follows:

1. ROW_NUMBER () function number, such as

select email,customerID, ROW_NUMBER() over(order by psd) as rows from QT_Customer

Principle: press psd sort, sort after, each data to be numbered.

2. In order to sort in ascending order of price, and sorting code to each record is as follows:

select DID,customerID,totalPrice,ROW_NUMBER() over(order by totalPrice) as rows from OP_Order

3. The statistics for each of each household and per amount of all orders in ascending order under the orders of a customer, at the same time a number to each customer's order. So we know a few orders of each customer.

Figure:

SQL Server database ROW_NUMBER () uses detailed

code show as below:

select ROW_NUMBER() over(partition by customerID  order by totalPrice) as rows,customerID,totalPrice, DID from OP_Order

4. Statistical each customer recently placed an order is an order under section several times.

SQL Server database ROW_NUMBER () uses detailed

code show as below:

 
  1.  with tabs as  
  2. (  
  3. select ROW_NUMBER() over(partition by customerID  order by totalPrice) as rows,customerID,totalPrice, DID from OP_Order  
  4.  )  
  5. select MAX (rows) as 'single number', customerID from tabs group by customerID 

5. The minimum amount of purchase statistics every customer all the orders and change orders and statistics, how many times customers are buying.

Figure:

SQL Server database ROW_NUMBER () uses detailed

The figure: rows represent customers many times to buy. 

Idea: to perform this operation using the temporary table.

1. The press group the customers, and then press the timing clients are sorted, and are numbered.

2. Then use the sub-query to find out the minimum price at which each customer purchase.

3. According to locate the appropriate records to find out the minimum price for each customer.

code show as below:

 
  1. with tabs as  
  2.  (  
  3. select ROW_NUMBER() over(partition by customerID  order by insDT) as rows,customerID,totalPrice, DID from OP_Order  
  4. )  
  5.  select * from tabs  
  6. where totalPrice in   
  7. (  
  8. select MIN(totalPrice)from tabs group by customerID  
  9.  ) 

6. screened for the first time under the orders of customers. 

SQL Server database ROW_NUMBER () uses detailed

Ideas. Use rows = 1 to query customer order records for the first time under.

code show as below:

 
  1. with tabs as  
  2. (  
  3. select ROW_NUMBER() over(partition by customerID  order by insDT) as rows,* from OP_Order  
  4. )  
  5. select * from tabs where rows = 
  6. select * from OP_Order 

7.rows_number () for paging

Ideas: first screened all the products come out, then these products are numbered. Then filtered in the where clause. 

8. Note: When using a windowing function over the like, performed over night grouping and ordering execution in the inside "where, group by, order by" a.

The following code:

 
  1. select   
  2. ROW_NUMBER() over(partition by customerID  order by insDT) as rows,  
  3. customerID,totalPrice, DID  
  4. from OP_Order where insDT>'2011-07-22' 

The above code is the first implementation of the where clause, after the implementation, to give each record are numbered.

Guess you like

Origin www.cnblogs.com/phpk/p/10935649.html