Тёмный

Grouping Sets in SQL Server 

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

grouping sets examples in sql server
sql server grouping sets order by grouping
sql server 2008 grouping sets example
sql server group by union all
Grouping sets is a new feature introduced in SQL Server 2008. Let us understand Grouping sets with an example.
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
We want to calculate Sum of Salary by Country and Gender.
We can very easily achieve this using a Group By query as shown below
Select Country, Gender, Sum(Salary) as TotalSalary
From Employees
Group By Country, Gender
Within the same result set we also want Sum of Salary just by Country.
To achieve the above result we could combine 2 Group By queries using UNION ALL as shown below.
Select Country, Gender, Sum(Salary) as TotalSalary
From Employees
Group By Country, Gender
UNION ALL
Select Country, NULL, Sum(Salary) as TotalSalary
From Employees
Group By Country
Within the same result set we also want Sum of Salary just by Gender.
We can achieve this by combining 3 Group By queries using UNION ALL as shown below
Select Country, Gender, Sum(Salary) as TotalSalary
From Employees
Group By Country, Gender
UNION ALL
Select Country, NULL, Sum(Salary) as TotalSalary
From Employees
Group By Country
UNION ALL
Select NULL, Gender, Sum(Salary) as TotalSalary
From Employees
Group By Gender
Finally we also want the grand total of Salary. In this case we are not grouping on any particular column. So both Country and Gender columns will be NULL in the resultset.
To achieve this we will have to combine the fourth query using UNION ALL as shown below.
Select Country, Gender, Sum(Salary) as TotalSalary
From Employees
Group By Country, Gender
UNION ALL
Select Country, NULL, Sum(Salary) as TotalSalary
From Employees
Group By Country
UNION ALL
Select NULL, Gender, Sum(Salary) as TotalSalary
From Employees
Group By Gender
UNION ALL
Select NULL, NULL, Sum(Salary) as TotalSalary
From Employees
There are 2 problems with the above approach.
The query is huge as we have combined different Group By queries using UNION ALL operator. This can grow even more if we start to add more groups
The Employees table has to be accessed 4 times, once for every query.
If we use Grouping Sets feature introduced in SQL Server 2008, the amount of T-SQL code that you have to write will be greatly reduced. The following Grouping Sets query produce the same result as the above UNION ALL query.
Select Country, Gender, Sum(Salary) TotalSalary
From Employees
Group BY
GROUPING SETS
(
(Country, Gender), -- Sum of Salary by Country and Gender
(Country), -- Sum of Salary by Country
(Gender) , -- Sum of Salary by Gender
() -- Grand Total
)
The order of the rows in the result set is not the same as in the case of UNION ALL query. To control the order use order by as shown below.
Select Country, Gender, Sum(Salary) TotalSalary
From Employees
Group BY
GROUPING SETS
(
(Country, Gender), -- Sum of Salary by Country and Gender
(Country), -- Sum of Salary by Country
(Gender) , -- Sum of Salary by Gender
() -- Grand Total
)
Order By Grouping(Country), Grouping(Gender), Gender
Text version of the video
csharp-video-tutorials.blogspo...
Slides
csharp-video-tutorials.blogspo...
All SQL Server Text Articles
csharp-video-tutorials.blogspo...
All SQL Server Slides
csharp-video-tutorials.blogspo...
All Dot Net and SQL Server Tutorials in English
ru-vid.com...
All Dot Net and SQL Server Tutorials in Arabic
/ kudvenkatarabic

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

 

18 сен 2015

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 47   
@krzysztofs8535
@krzysztofs8535 7 лет назад
Venkat rules. I'm the biggest fan of your videos. Thank U for educating community !
@arunshivaraaj7242
@arunshivaraaj7242 3 месяца назад
Excellent teaching
@longlivesteelo1468
@longlivesteelo1468 2 года назад
Can't believe I'm paying 500€/month for a post-graduate degree and I have to search youtube for explanations. Thank you for your help :)
@drstrangelove9851
@drstrangelove9851 Месяц назад
Most of what I've learned in programming has been after earning my CS degree.
@Tabla_Tejas67
@Tabla_Tejas67 Год назад
Great explanationThank you sir
@ga7853
@ga7853 7 лет назад
Venkat, Really really thank you, for introducing all these new stuff "Grouping Sets" and "Order by Grouping()" .. God bless
@nickt423
@nickt423 3 года назад
Venkat, you are a legend bro!
@sj7578
@sj7578 8 лет назад
A Great Teaching Video! THX
@anaisrevellat5692
@anaisrevellat5692 Год назад
I just want to say that it is very very well explained, following courses I had to come here to deeply understand, thank you.
@rudiantoxu2497
@rudiantoxu2497 6 месяцев назад
really helped me to undertand the way of using grouping sets
@479210251
@479210251 5 лет назад
very helpful, you are at least 10* better at explaining than my lecturer.
@minhhuynhthe
@minhhuynhthe Год назад
thank you so much, I have stucked with this for a few days.
@chowjiayi1644
@chowjiayi1644 Год назад
This helped me understand grouping sets from my sql course, thank you so much!
@idioticthings4517
@idioticthings4517 7 лет назад
Thank You Sir............... Your tutorials make my career
@vzntoup
@vzntoup 3 года назад
You definitely don't get all the publicity you deserve ... well done and thank you a lot for your time and effort!
@eespiedrop2
@eespiedrop2 7 лет назад
A great explanation, well done and thanks. Very clear and concise.
@pandianputube
@pandianputube 6 лет назад
Nice one Venkat, Thanks!
@anikethirve2557
@anikethirve2557 8 лет назад
Very neatly explained! :)
@adilandzoyaenglishlearning9232
Excellent way of Teaching. I Love it
@djoharmouhoumed847
@djoharmouhoumed847 8 лет назад
thank you so much very good explication i realy apprecied
@rgarayia
@rgarayia 8 лет назад
Thank you so much. Very nicely explained.
@MiaryMathieu
@MiaryMathieu 7 лет назад
Thank you so much! :)
@hajeraabdullah9157
@hajeraabdullah9157 4 года назад
you're lovely - simple and clear
@francisdrake7000
@francisdrake7000 3 года назад
good one
@programmingjo
@programmingjo 8 лет назад
Bravo!!! thank you so much!
@Csharp-video-tutorialsBlogspot
+Jorge Mongelos You are very welcome and thank you so much for the feedback. Means a lot. Glad the videos are useful. Dot Net & SQL Server training videos for aspiring web developers ru-vid.complaylists?view=1&sort=dd Download our training videos and slides for offline viewing www.pragimtech.com/Order.aspx Code Samples, Text Version of the videos & PPTS on my blog csharp-video-tutorials.blogspot.com Tips to best utilise 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 using the link below ru-vid.com Please click the THUMBS UP button below the video, if you think you liked them Thank you for sharing the link with your friends who you think would also benefit from them Best Venkat
@carmelraj2158
@carmelraj2158 7 лет назад
great. Thanks a lot
@majdalhafi3475
@majdalhafi3475 2 года назад
Great jop ..
@gustavoalbor9188
@gustavoalbor9188 7 лет назад
THANK YOU!!
@nys8260
@nys8260 3 года назад
Thanks venkat
@nazrinca9664
@nazrinca9664 3 года назад
VERY GOOD LECTURE
@mr.prasadyadav
@mr.prasadyadav 3 года назад
Very nice Lecture
@chandanagarwal9313
@chandanagarwal9313 7 лет назад
thanks Sir :)
@imrantech3065
@imrantech3065 3 года назад
Thankyou
@beepst
@beepst 6 лет назад
Thank you
@abrooabru8863
@abrooabru8863 8 лет назад
can u share about performance demerits alongwith joins for grouping sets ?
@sarmiento42
@sarmiento42 3 года назад
graciassssssssssssssssssss
@gavtronics
@gavtronics 3 года назад
Imagine waking up in the morning as Venkat, realizing who you are, and knowing you are a good person.
@ashishrao5635
@ashishrao5635 8 лет назад
Hi, Could you also explain how to us Red Gate s/w for transferring data from one server to another
@umeshthemaster
@umeshthemaster 8 лет назад
plz make sql server playlist because there r other playlists u have made like jquery. u r awesome sir
@umeshthemaster
@umeshthemaster 8 лет назад
Richard Vanni ok..got it...dont know why i cudnt find it earlier
@arun_sql_msbi480
@arun_sql_msbi480 5 лет назад
How to find emp name with each department with lowest attendance in the month of December
@arun_sql_msbi480
@arun_sql_msbi480 5 лет назад
How to find employee name with each department with lowest attendance in the all months of the year
@ManishKumar-qx1kh
@ManishKumar-qx1kh 3 года назад
@@arun_sql_msbi480 you can use group by on the month or month+year
@sunnishReddy6105
@sunnishReddy6105 7 лет назад
whats the difference using group by and grouping sets?
@abhinavbhardwaj9158
@abhinavbhardwaj9158 3 года назад
I am not clear with the order by used by group sets . How it worked ??
@sunnishReddy6105
@sunnishReddy6105 7 лет назад
whats the difference using group by and grouping sets?