Тёмный

NTILE function in SQL Server 

kudvenkat
Подписаться 835 тыс.
Просмотров 95 тыс.
50% 1

In this video we will discuss NTILE function in SQL Server
NTILE function
1. Introduced in SQL Server 2005
2. ORDER BY Clause is required
3. PARTITION BY clause is optional
4. Distributes the rows into a specified number of groups
5. If the number of rows is not divisible by number of groups, you may have groups of two different sizes.
6. Larger groups come before smaller groups
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our RU-vid channel. Hope you can help.
/ @aarvikitchen5572
For example
NTILE(2) of 10 rows divides the rows in 2 Groups (5 in each group)
NTILE(3) of 10 rows divides the rows in 3 Groups (4 in first group, 3 in 2nd & 3rd group)
Syntax : NTILE (Number_of_Groups) OVER (ORDER BY Col1, Col2, ...)
SQL Script to create Employees table
Create Table Employees
(
Id int primary key,
Name nvarchar(50),
Gender nvarchar(10),
Salary int
)
Go
Insert Into Employees Values (1, 'Mark', 'Male', 5000)
Insert Into Employees Values (2, 'John', 'Male', 4500)
Insert Into Employees Values (3, 'Pam', 'Female', 5500)
Insert Into Employees Values (4, 'Sara', 'Female', 4000)
Insert Into Employees Values (5, 'Todd', 'Male', 3500)
Insert Into Employees Values (6, 'Mary', 'Female', 5000)
Insert Into Employees Values (7, 'Ben', 'Male', 6500)
Insert Into Employees Values (8, 'Jodi', 'Female', 7000)
Insert Into Employees Values (9, 'Tom', 'Male', 5500)
Insert Into Employees Values (10, 'Ron', 'Male', 5000)
Go
NTILE function without PARTITION BY clause : Divides the 10 rows into 3 groups. 4 rows in first group, 3 rows in the 2nd & 3rd group.
SELECT Name, Gender, Salary,
NTILE(3) OVER (ORDER BY Salary) AS [Ntile]
FROM Employees
What if the specified number of groups is GREATER THAN the number of rows
NTILE function will try to create as many groups as possible with one row in each group.
With 10 rows in the table, NTILE(11) will create 10 groups with 1 row in each group.
SELECT Name, Gender, Salary,
NTILE(11) OVER (ORDER BY Salary) AS [Ntile]
FROM Employees
NTILE function with PARTITION BY clause : When the data is partitioned, NTILE function creates the specified number of groups with in each partition.
The following query partitions the data into 2 partitions (Male & Female). NTILE(3) creates 3 groups in each of the partitions.
SELECT Name, Gender, Salary,
NTILE(3) OVER (PARTITION BY GENDER ORDER BY Salary) AS [Ntile]
FROM Employees
Link for all dot net and sql server video tutorial playlists
www.youtube.co...
Link for slides, code samples and text version of the video
csharp-video-tu...

Опубликовано:

 

5 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 33   
@krzysztofs8535
@krzysztofs8535 7 лет назад
Thank U for educating community. You are the best teacher on the Earth. God bless U and keep goin'. Best regards from beautiful Poland.
@jimmyjamestruscott
@jimmyjamestruscott 6 лет назад
Of all of the SQL tutorials I have watched on youtube, your channel is the best. Great work mate!
@tommartin3883
@tommartin3883 7 лет назад
I'm impressed with both the amount and quality of instructional videos you have created. I've found these videos HUGELY valuable in improving my understanding of a number of SQL functions. Thank you very much for sharing your knowledge.
@maniero1980
@maniero1980 9 лет назад
Thank you for this great tutorial, you are the best. thanks man
@Csharp-video-tutorialsBlogspot
+Papa Collabo Thank you very much for taking time to give feedback. This means a lot. I am very glad you found the videos useful. Dot Net & SQL Server training videos for web developers ru-vid.complaylists?view=1&sort=dd You can order DVDs for offline viewing using the link below www.pragimtech.com/Order.aspx Code Samples & Slides are on my blog csharp-video-tutorials.blogspot.com Tips to effectively use our channel ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-y780MwhY70s.html To receive email alerts, when new videos are uploaded, please subscribe to our channel ru-vid.com Please click that THUMBS UP button below the video, if you like the videos Thanks a million for sharing these resources with your friends Best Venkat
@girijesh-mca
@girijesh-mca 9 лет назад
Thank you so much sir your all videos is awesome !!! and these are very helpful to clear my logic.
@timstrack4573
@timstrack4573 4 года назад
This is great! I'm taking an on line SQL and the instructor makes the mat'l too diffucult, I've been using your videos to actually understand the function.
@prasannag8600
@prasannag8600 7 лет назад
Best teacher I ever had in my life....awesome explanation
@vikas.dsalve
@vikas.dsalve 9 лет назад
Thank You sir for uploading this video it's clear my concept
@alongzhang7241
@alongzhang7241 3 года назад
Hi This is well explained!!! Very clear and easy to follow your steps
@raqibulAlam-su3jo
@raqibulAlam-su3jo 9 лет назад
Nice Tutorial.Go Ahead.
@NayanajithPilapitiya
@NayanajithPilapitiya 9 лет назад
Thank you very much and it was really interesting. :)
@hmvartak
@hmvartak 9 лет назад
Nice video! Thanks!
@Abdullah_Khaled
@Abdullah_Khaled Год назад
Just to the point, Thanks
@sdharitri
@sdharitri Год назад
very well explained. Thanks😊
@govindrajkannan7757
@govindrajkannan7757 9 лет назад
Thank u very much sir
@subhantbanerjee1692
@subhantbanerjee1692 Год назад
Very well explained.
@couragemath9333
@couragemath9333 6 лет назад
Awesome. Thank you Sir!
@nilanjansarkar100
@nilanjansarkar100 5 лет назад
Amazing clarity!
@nys8260
@nys8260 3 года назад
Thank you very much KUDVENKAT
@noronhasanjay
@noronhasanjay 2 года назад
Thanks Venkat
@varuns4472
@varuns4472 2 года назад
Super explanation..
@rogersantos2605
@rogersantos2605 9 лет назад
Thanks, nice explaning !!
@krutomjer
@krutomjer 9 лет назад
Thanks man !
@LVS311
@LVS311 3 года назад
Thanks for the explaination Sir.. But, We could see in the output that rows with salary 5000 were having NTILE value 1 as well as 2.. After all, NTILE was ordered by Salary Column.. That was a bit confusing part..
@giorgosi.fovakis8810
@giorgosi.fovakis8810 3 года назад
Thank you a lot!
@frenkyb123
@frenkyb123 5 лет назад
Where would NTILE function be usable in everyday practise?
@raypul1
@raypul1 8 лет назад
Thank you very much...
@archrodney
@archrodney 7 лет назад
What is the real world use case of such grouping?
@tiagosilva856
@tiagosilva856 2 года назад
Ntile can performance equal to row_num if we create the number of groups equal to the number of rows
@saichaitanya154
@saichaitanya154 Год назад
Dude speaks in lowercase!
@socratesuffer2765
@socratesuffer2765 7 лет назад
nice videos. what is the business needs for ntile function?
@yaravallirohit8014
@yaravallirohit8014 2 года назад
I'm impressed with both the amount and quality of instructional videos you have created. I've found these videos HUGELY valuable in improving my understanding of a number of SQL functions. Thank you very much for sharing your knowledge.
Далее
Lead and Lag functions in SQL Server 2012
7:27
Просмотров 160 тыс.
Over clause in SQL Server
9:13
Просмотров 240 тыс.
Песня РАСПУТИН на русском!🔥
00:56
Difference between rows and range
5:52
Просмотров 85 тыс.
SQL Tutorial - How to use NTILE in SQL Server
15:28
Просмотров 4,9 тыс.
Window functions in SQL Server
11:00
Просмотров 220 тыс.
Offset fetch next in SQL Server 2012
9:04
Просмотров 73 тыс.
Dynamic SQL in SQL Server
12:12
Просмотров 134 тыс.