How to Implement NTILE functions in SQL Server 2000

Source: Internet
Author: User

Each group is numbered from the beginning. For each row, the NTILE function returns the ID of the group to which the row belongs. If the number of rows in a partition cannot be divisible by integers, a Member may have two groups of different sizes. In the order specified by the OVER clause, a large group is placed before a small group. Using the NTILE function to calculate the ranking value is the same as other methods. Simple partition ranking scheme and ranking value efficiency analysis graphic + test code]), the only difference is that, the NTILE function accepts a parameter that represents the number of groups, while other methods do not.

The SQL code and effects are as follows:

 
 
  1. Code  
  2. ifOBJECT_ID('Sales')isnotnull  
  3. droptableSales;  
  4. createtableSales  
  5. (  
  6. empidvarchar(10)notnullprimarykey,  
  7. qtyintnotnull  
  8. )  
  9. insertintoSales(empid,qty)values('A',300);  
  10. insertintoSales(empid,qty)values('B',100);  
  11. insertintoSales(empid,qty)values('C',200);  
  12. insertintoSales(empid,qty)values('D',200);  
  13. insertintoSales(empid,qty)values('E',250);  
  14. insertintoSales(empid,qty)values('F',300);  
  15. insertintoSales(empid,qty)values('H',250);  
  16. insertintoSales(empid,qty)values('I',250);  
  17. insertintoSales(empid,qty)values('J',100);  
  18. insertintoSales(empid,qty)values('K',200);  
  19. insertintoSales(empid,qty)values('G',100);  
  20. --------------------------------------------------  
  21. selectempid,qty,NTILE(9)over(orderbyqty)astilefromSales 

Execution result:

So how did we achieve this effect before SQLServer2005 appeared? Below I will introduce two methods to meet this requirement:

Method 1: Calculate the row number ranking value of the table first), and obtain the number of records in each group based on the number of specified groups. The group number calculation formula is as follows: row number-1)/group size + 1. The group number of each record is returned.

The SQL code is as follows:

 
 
  1. Code
  2. Eclare @ numtilesint;
  3. Set@ Numtiles = 9;-- Number of groups 
  4.  
  5. Selectempid, qty,CAST(Rn-1)/tilesize + 1 asint) astile
  6. From(Selectempid, qty, rn, 1.0 * numrows/@ numtilesastilesizefrom (selectempid, qty, (selectCOUNT (*) fromSalesasS2whereS2. qty

Method 2: Calculate the row number ranking value of the table first), and obtain the number of records in each group based on the number of specified groups. Then, use the following group number calculation formula to return the group number of each record.

If (row number <= group size + 1) * Number of remaining rows) then

Group number = row number-1)/group size + 1) + 1

Else

Group number = row number-remaining lines-1)/group size + 1

The SQL code is as follows:

 
 
  1. Code
  2. Declare@ Numtileint;
  3. Set@ Numtile = 9;-- Number of groups 
  4.  
  5. Selectempid, qty, rn,
  6. Casewhenrn <= (tilesize + 1) * remainder
  7. ThenRn-1/(tilesize + 1) + 1
  8. ElseRn-remainder-1/(tilesize) + 1
  9. Endastiles
  10. From 
  11. (
  12. Selectempid, qty, rn, numrows/@ numtileastilesize, numrows % @ numtileasremainder
  13. From 
  14. (
  15. Selectempid, qty, (selectCOUNT (*) fromSalesasS2whereS2. qty
  16. ) AsD1
  17. ) AsD2orderbyqty, empid
  1. 10 key features of SQL Server 2005 Business Intelligence
  2. Notes for using temporary tables in a T-SQL
  3. SQL Server database management common SQL and T-SQL statements (1)
  4. Interview SQL Server developers with T-SQL operations (1)
  5. T-SQL in SQL Server 2005
  6. T-SQL

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.