SQL Tutorial - How to use NTILE in SQL Server

  Рет қаралды 4,848

BeardedDev

BeardedDev

Күн бұрын

Another video brought to you by BeardedDev, bringing you tutorials on Business Intelligence, SQL Programming and Data Analysis.
You can now support me on patreon - / beardeddev
To improve your T-SQL skills check out my recommended reading list:
T-SQL Querying
www.amazon.com/gp/product/073...
T-SQL Fundamentals
www.amazon.com/gp/product/150...
Microsoft SQL Server 2012 High-Performance T-SQL Using Window Functions
www.amazon.com/gp/product/073...
In this SQL Tutorial we cover an overview of NTILE and the syntax, a discussion of the difference between partitions and tiles. We also demonstrate a particularly poor example of using NTILE and typical use cases.
To follow along with the video please use scripts below:
IF OBJECT_ID(N'dbo.Sale', N'U') IS NOT NULL
DROP TABLE dbo.Sale
IF OBJECT_ID(N'dbo.SaleChannel', N'U') IS NOT NULL
DROP TABLE dbo.SaleChannel
CREATE TABLE SaleChannel
(
SaleChannelId INT NOT NULL IDENTITY(1, 1)
CONSTRAINT PK_SalesChannel_SaleChannelId PRIMARY KEY (SaleChannelId)
, SaleChannel VARCHAR(10) NOT NULL
)
INSERT INTO dbo.SaleChannel (SaleChannel)
VALUES ('In-Store'), ('Online')
CREATE TABLE Sale
(
SaleId INT NOT NULL IDENTITY(1, 1)
CONSTRAINT PK_Sales_SaleId PRIMARY KEY (SaleId)
, SaleChannelId INT NOT NULL
CONSTRAINT FK_Sales_SaleChannelId FOREIGN KEY (SaleChannelId) REFERENCES SaleChannel (SaleChannelId)
, SaleDate DATE NOT NULL
, SaleAmount DECIMAL(6, 2) NOT NULL
)
INSERT INTO dbo.Sale (SaleChannelId, SaleDate, SaleAmount)
VALUES (1, '20180101', 15.99)
, (1, '20180201', 7.49)
, (1, '20180301', 14.99)
, (1, '20180401', 6.49)
, (1, '20180501', 13.99)
, (1, '20180601', 5.49)
, (1, '20180701', 19.99)
, (1, '20180801', 10.49)
, (1, '20180901', 18.99)
, (1, '20181001', 11.49)
, (1, '20181101', 17.99)
, (1, '20181201', 12.49)
, (2, '20180101', 16.99)
, (2, '20180201', 13.49)
, (2, '20180301', 15.99)
, (2, '20180401', 14.49)
, (2, '20180501', 19.99)
, (2, '20180601', 7.49)

Пікірлер: 13
@sujaybshah
@sujaybshah 3 жыл бұрын
You're a gem in explaining window functions(so far I checked out the SQL window functions video, will check out others soon)
@OldPick-Unix-dude-pb9jg
@OldPick-Unix-dude-pb9jg 10 ай бұрын
Appreciate the explanation on the NTILE - used it create sample data.
@BeardedDevData
@BeardedDevData 10 ай бұрын
Excellent.
@pokepoke3
@pokepoke3 4 жыл бұрын
I just started learning about window functions, and you've been the most helpful. Thank you.
@giorgosi.fovakis8810
@giorgosi.fovakis8810 3 жыл бұрын
Great! and nice example with CTE in the end as well
@amalal-suhaimi9810
@amalal-suhaimi9810 4 жыл бұрын
Can you provide a table with the movie titles and divide them into 4 levels (first_quarter, second_quarter, third_quarter, and final_quarter) based on the quartiles (25%, 50%, 75%) of the rental duration for movies across all categories? NTILE(4) OVER (ORDER BY title ) AS standard_quartiles >>>> is it right ?
@BeardedDevData
@BeardedDevData 4 жыл бұрын
In that case you will need to change the order by to be by rental duration.
@Khaled_Abdelaal
@Khaled_Abdelaal 3 жыл бұрын
@BeardedDev: I have one technical question. How can I contact you, please?
@BeardedDevData
@BeardedDevData 3 жыл бұрын
Hi, you can either contact me on here or email me at askbeardeddev@gmail.com
@Khaled_Abdelaal
@Khaled_Abdelaal 3 жыл бұрын
@@BeardedDevData Thanks. I will drop you an email
@pabeader1941
@pabeader1941 3 жыл бұрын
I know this is an old video, but did you know that it cuts off before you complete the story about the 1 million rows of data??
@BeardedDevData
@BeardedDevData 3 жыл бұрын
Yes, there was an issue with the end screen but because I had worked through all the examples it was uploaded. The story regarding the million rows actually finishes, I just explain that if you have that many rows you can divide them in to tiles then select a tile to work with.
@pabeader1941
@pabeader1941 3 жыл бұрын
@@BeardedDevData okay. I'm just used to your wind down at the end. sall gud.
SQL Tutorial - Window Functions (Follow Along)
23:16
BeardedDev
Рет қаралды 6 М.
SQL Tutorial - Window Functions - Ranking
17:32
BeardedDev
Рет қаралды 30 М.
Little girl's dream of a giant teddy bear is about to come true #shorts
00:32
Получилось у Миланы?😂
00:13
ХАБИБ
Рет қаралды 4 МЛН
What it feels like cleaning up after a toddler.
00:40
Daniel LaBelle
Рет қаралды 83 МЛН
Stay on your way 🛤️✨
00:34
A4
Рет қаралды 22 МЛН
Database Indexing for Dumb Developers
15:59
Laith Academy
Рет қаралды 48 М.
SQL Tutorial - Creating a sample of data
10:48
BeardedDev
Рет қаралды 7 М.
How I use SQL as a Data Analyst
15:30
Luke Barousse
Рет қаралды 814 М.
SQL Tutorial - Difference between CTEs and Derived Tables
28:53
BeardedDev
Рет қаралды 2,6 М.
How To Load One BILLION Rows into an SQL Database
12:17
Database Star
Рет қаралды 28 М.
T-SQL Tutorial - PIVOT without using PIVOT
15:43
BeardedDev
Рет қаралды 5 М.
Expert Level SQL Tutorial
23:27
James Oliver
Рет қаралды 153 М.
Samsung laughing on iPhone #techbyakram
0:12
Tech by Akram
Рет қаралды 6 МЛН
low battery 🪫
0:10
dednahype
Рет қаралды 1,2 МЛН
Хакер взломал компьютер с USB кабеля. Кевин Митник.
0:58
Последний Оплот Безопасности
Рет қаралды 1,6 МЛН
iPhone 15 Pro Max vs IPhone Xs Max  troll face speed test
0:33