Тёмный

Grouping function in SQL Server 

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

sql server rollup grouping
sql server rollup replace null
sql server rollup grouping replace null
In this video we will discuss the use of Grouping function in SQL Server.
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
This is continuation to Part 104. Please watch Part 104 from SQL Server tutorial before proceeding.
What is Grouping function
Grouping(Column) indicates whether the column in a GROUP BY list is aggregated or not. Grouping returns 1 for aggregated or 0 for not aggregated in the result set.
The following query returns 1 for aggregated or 0 for not aggregated in the result set
SELECT Continent, Country, City, SUM(SaleAmount) AS TotalSales,
GROUPING(Continent) AS GP_Continent,
GROUPING(Country) AS GP_Country,
GROUPING(City) AS GP_City
FROM Sales
GROUP BY ROLLUP(Continent, Country, City)
What is the use of Grouping function in real world
When a column is aggregated in the result set, the column will have a NULL value. If you want to replace NULL with All then this GROUPING function is very handy.
SELECT CASE WHEN
GROUPING(Continent) = 1 THEN 'All' ELSE ISNULL(Continent, 'Unknown')
END AS Continent,
CASE WHEN
GROUPING(Country) = 1 THEN 'All' ELSE ISNULL(Country, 'Unknown')
END AS Country,
CASE
WHEN GROUPING(City) = 1 THEN 'All' ELSE ISNULL(City, 'Unknown')
END AS City,
SUM(SaleAmount) AS TotalSales
FROM Sales
GROUP BY ROLLUP(Continent, Country, City)
Can't I use ISNULL function instead as shown below
SELECT ISNULL(Continent, 'All') AS Continent,
ISNULL(Country, 'All') AS Country,
ISNULL(City, 'All') AS City,
SUM(SaleAmount) AS TotalSales
FROM Sales
GROUP BY ROLLUP(Continent, Country, City)
Well, you can, but only if your data does not contain NULL values. Let me explain what I mean.
At the moment the raw data in our Sales has no NULL values. Let's introduce a NULL value in the City column of the row where Id = 1
Update Sales Set City = NULL where Id = 1
Now execute the following query with ISNULL function
SELECT ISNULL(Continent, 'All') AS Continent,
ISNULL(Country, 'All') AS Country,
ISNULL(City, 'All') AS City,
SUM(SaleAmount) AS TotalSales
FROM Sales
GROUP BY ROLLUP(Continent, Country, City)
Notice that the actuall NULL value in the raw data is also replaced with the word 'All', which is incorrect. Hence the need for Grouping function.
Please note : Grouping function can be used with Rollup, Cube and Grouping Sets
Text version of the video
csharp-video-tu...
Slides
csharp-video-tu...
All SQL Server Text Articles
csharp-video-tu...
All SQL Server Slides
csharp-video-tu...
All Dot Net and SQL Server Tutorials in English
www.youtube.co...
All Dot Net and SQL Server Tutorials in Arabic
/ kudvenkatarabic

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

 

5 окт 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 14   
@krzysztofs8535
@krzysztofs8535 7 лет назад
Venkat rules! Thank you for educating community. I'm the biggest fan of this tutorial I watched all your videos. You are a legend.
@MrCardeso
@MrCardeso 9 лет назад
Thank you. I have greatly enjoyed the last few videos on SQL and learned quite a bit.
@88spaces
@88spaces 5 лет назад
kudvenkat, You are impressive. Lov your videos.
@ajmohd7316
@ajmohd7316 9 лет назад
Thanks. This video was very helpful.
@ihabfam1059
@ihabfam1059 5 лет назад
GOOD JOB, EASY TO UNDERSTAND
@balakrishnareddithala8718
@balakrishnareddithala8718 Год назад
thank you teacher...
@raqibul1000
@raqibul1000 9 лет назад
Thanks A Lot.
@hyd-kn4si
@hyd-kn4si 8 лет назад
You are a legend
@Csharp-video-tutorialsBlogspot
+hyd 2016 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
@muhammadrehbarsheikh8498
@muhammadrehbarsheikh8498 9 лет назад
Thanks sir!!
@abhaverma557
@abhaverma557 8 лет назад
Nice Video...Just one doubt. I read oracle book and it shows that return value 1 is when the expression has not been used to calculate the aggregate value whereas you are telling other way around. Please explain.
@PinasPiliNa999
@PinasPiliNa999 9 лет назад
Hi Venkat, May I kidnly request if you can make a video on how to calculate a running total based on two columns Debit and Credit and put it in a third field called balance to be grouped by MemberID or DepositorID. Thanks a lot!
@durgapalepu5780
@durgapalepu5780 9 лет назад
+Renoir Rapido You can achieve this by using raking function SUM() OVER() with CASE statement.
@PinasPiliNa999
@PinasPiliNa999 9 лет назад
Hi Durga, Thanks for the suggestion, I tried but could not get it, may I request if you can send a sample script?Thanks!
Далее
GROUPING ID function in SQL Server
12:37
Просмотров 60 тыс.
НЕ БУДИТЕ КОТЯТ#cat
00:21
Просмотров 1 млн
SubTotals with SQL using ROLLUP
7:06
Просмотров 10 тыс.
69 What is Stuff function in sql server
13:02
Просмотров 1,7 тыс.
Stored procedures in sql server   Part 18
20:11
Просмотров 1,5 млн
Rank and Dense Rank in SQL Server
10:08
Просмотров 240 тыс.
Window functions in SQL Server
11:00
Просмотров 220 тыс.
Learn Database Normalization - 1NF, 2NF, 3NF, 4NF, 5NF
28:34