NTILE used to distributes the rows into a specified number of groups.
Let's
Test It....
Step
1:- Create Table  
CREATE TABLE TEST (
   ID INT,
   NAME VARCHAR(100)
);
Step
2:- Insert Records in the Table
INSERT INTO TEST VALUES(1,'Apple');
INSERT INTO TEST VALUES(2,'Apricot');
INSERT INTO TEST VALUES(3,'Avocado');
INSERT INTO TEST VALUES(4,'Banana');
INSERT INTO TEST VALUES(5,'Blackberry');
INSERT INTO TEST VALUES(6,'Blackplum');
INSERT INTO TEST VALUES(7,'Breadfruit');
INSERT INTO TEST VALUES(8,'Chickoo');
INSERT INTO TEST VALUES(9,'Cucumber');
INSERT INTO TEST VALUES(10,'Custardapple');
INSERT INTO TEST VALUES(11,'Dates');
INSERT INTO TEST VALUES(12,'Dragonfruit');
INSERT INTO TEST VALUES(13,'Figs');
INSERT INTO TEST VALUES(14,'Grapes');
INSERT INTO TEST VALUES(15,'Guavas');
INSERT INTO TEST VALUES(16,'Mango');
INSERT INTO TEST VALUES(17,'Orange');
INSERT INTO TEST VALUES(18,'Papaya');
INSERT INTO TEST VALUES(19,'Pear');
INSERT INTO TEST VALUES(20,'Pineapple');
INSERT INTO TEST VALUES(21,'Plum');
INSERT INTO TEST VALUES(22,'Pomogranate');
INSERT INTO TEST VALUES(23,'Watermelon');
Step
3:- Select Records
SELECT * FROM TEST;
| 
   
ID 
 | 
  
   
NAME 
 | 
 
| 
   
1 
 | 
  
   
Apple 
 | 
 
| 
   
2 
 | 
  
   
Apricot 
 | 
 
| 
   
3 
 | 
  
   
Avocado 
 | 
 
| 
   
4 
 | 
  
   
Banana 
 | 
 
| 
   
5 
 | 
  
   
Blackberry 
 | 
 
| 
   
6 
 | 
  
   
Blackplum 
 | 
 
| 
   
7 
 | 
  
   
Breadfruit 
 | 
 
| 
   
8 
 | 
  
   
Chickoo 
 | 
 
| 
   
9 
 | 
  
   
Cucumber 
 | 
 
| 
   
10 
 | 
  
   
Custardapple 
 | 
 
| 
   
11 
 | 
  
   
Dates 
 | 
 
| 
   
12 
 | 
  
   
Dragonfruit 
 | 
 
| 
   
13 
 | 
  
   
Figs 
 | 
 
| 
   
14 
 | 
  
   
Grapes 
 | 
 
| 
   
15 
 | 
  
   
Guavas 
 | 
 
| 
   
16 
 | 
  
   
Mango 
 | 
 
| 
   
17 
 | 
  
   
Orange 
 | 
 
| 
   
18 
 | 
  
   
Papaya 
 | 
 
| 
   
19 
 | 
  
   
Pear 
 | 
 
| 
   
20 
 | 
  
   
Pineapple 
 | 
 
| 
   
21 
 | 
  
   
Plum 
 | 
 
| 
   
22 
 | 
  
   
Pomogranate 
 | 
 
| 
   
23 
 | 
  
   
Watermelon 
 | 
 
Step
4:- Select Records with group
SELECT ID, NAME, NTILE(5) OVER(ORDER BY ID ASC) AS 'NTILE'
FROM TEST;
| 
   
ID 
 | 
  
   
NAME 
 | 
  
   
NTILE 
 | 
 
| 
   
1 
 | 
  
   
Apple 
 | 
  
   
1 
 | 
 
| 
   
2 
 | 
  
   
Apricot 
 | 
  
   
1 
 | 
 
| 
   
3 
 | 
  
   
Avocado 
 | 
  
   
1 
 | 
 
| 
   
4 
 | 
  
   
Banana 
 | 
  
   
1 
 | 
 
| 
   
5 
 | 
  
   
Blackberry 
 | 
  
   
1 
 | 
 
| 
   
6 
 | 
  
   
Blackplum 
 | 
  
   
2 
 | 
 
| 
   
7 
 | 
  
   
Breadfruit 
 | 
  
   
2 
 | 
 
| 
   
8 
 | 
  
   
Chickoo 
 | 
  
   
2 
 | 
 
| 
   
9 
 | 
  
   
Cucumber 
 | 
  
   
2 
 | 
 
| 
   
10 
 | 
  
   
Custardapple 
 | 
  
   
2 
 | 
 
| 
   
11 
 | 
  
   
Dates 
 | 
  
   
3 
 | 
 
| 
   
12 
 | 
  
   
Dragonfruit 
 | 
  
   
3 
 | 
 
| 
   
13 
 | 
  
   
Figs 
 | 
  
   
3 
 | 
 
| 
   
14 
 | 
  
   
Grapes 
 | 
  
   
3 
 | 
 
| 
   
15 
 | 
  
   
Guavas 
 | 
  
   
3 
 | 
 
| 
   
16 
 | 
  
   
Mango 
 | 
  
   
4 
 | 
 
| 
   
17 
 | 
  
   
Orange 
 | 
  
   
4 
 | 
 
| 
   
18 
 | 
  
   
Papaya 
 | 
  
   
4 
 | 
 
| 
   
19 
 | 
  
   
Pear 
 | 
  
   
4 
 | 
 
| 
   
20 
 | 
  
   
Pineapple 
 | 
  
   
5 
 | 
 
| 
   
21 
 | 
  
   
Plum 
 | 
  
   
5 
 | 
 
| 
   
22 
 | 
  
   
Pomogranate 
 | 
  
   
5 
 | 
 
| 
   
23 
 | 
  
   
Watermelon 
 | 
  
   
5 
 | 
 
Note:-
NTILE(N) - N indicate dividing rows
into number of groups.
No comments:
Post a Comment