Grouping function in SQL Server — Transcript
Full transcript
- 0:00this is part 105 of SQL Server tutorial
- 0:03in this video we'll discuss grouping
- 0:05function in SQL Server this is
- 0:07continuation to part 104 so please watch
- 0:09part 104 before proceeding what is
- 0:12grouping function grouping function
- 0:14indicates whether the column in a group
- 0:15by list is aggregated or not grouping
- 0:19returns one for aggregated or zero for
- 0:21not aggregated in the result set let's
- 0:24understand what we mean by the statement
- 0:25with an example here we have total sales
- 0:29aggregated across these three columns
- 0:31continent country and city and then we
- 0:34have three other columns here so
- 0:35basically these three columns are going
- 0:37to tell us whether if this total sales
- 0:40value is aggregated across any of these
- 0:44columns so if you look at the first row
- 0:46this total sales figure is not
- 0:49aggregated by any specific column so
- 0:52notice gpor continent gpor country and
- 0:54gpor city all of them are zero
- 0:57indicating that data is not aggregated
- 1:00for any of those columns whereas if you
- 1:02look at this third row look at that city
- 1:05is null so this subtotal here 3,000 this
- 1:09total sales is for all cities within
- 1:12India country that is present in Asia
- 1:14continent so this figure is aggregated
- 1:18across City column so notice gpor city
- 1:22is one whereas the other two columns are
- 1:25zero now if you look at this row right
- 1:28here this subtotal is for all cities and
- 1:31for all countries in Asia continent and
- 1:34if you look at the respect to gpor
- 1:36columns here notice country and city are
- 1:39one whereas continent is zero and if you
- 1:42look at the last row here all of the
- 1:44column values are null so this is the
- 1:47grand total so in this case the data is
- 1:51aggregated across all those three
- 1:52columns notice GP uncore column values
- 1:56here for all the columns are ones so
- 1:59let's see how to produce this output
- 2:01using this grouping function so this
- 2:04function is going to return one if the
- 2:06data is aggregated across the column
- 2:09that we specify otherwise it's going to
- 2:11return zero so let's flip to SQL
- 2:14management studio so this is the same
- 2:16query that we worked with in the
- 2:17previous video session so when we
- 2:19execute this we get the total sales
- 2:21grouped by continent country and city
- 2:25now we also want these three columns
- 2:28gpor continent GP country and gpor city
- 2:32for that we can use the grouping
- 2:34function now one important thing to keep
- 2:36in mind is that with this grouping
- 2:37function you can only specify one column
- 2:40that means here to get these three
- 2:42columns you will have to use grouping
- 2:44function three times so let's look at
- 2:46that in action
- 2:48now so along with the sale amount we
- 2:52want to use the grouping function and
- 2:55I'm going to
- 2:56pass
- 2:58continent and let's give it written alas
- 3:03gpor
- 3:05continent and similarly we want the same
- 3:07thing for country and
- 3:10city so grouping of country as gpor
- 3:17country
- 3:18and grouping of city as
- 3:24gpor City so let's execute this and
- 3:29notice that if data is aggregated across
- 3:31a column we get one for that gpor column
- 3:36if it's not aggregated we get zero all
- 3:40right now the next obvious question that
- 3:42comes to our mind is okay I know what is
- 3:44grouping function but what is the use of
- 3:47this grouping function in the real world
- 3:50now look at this when we have you know
- 3:54this total sales amount aggregated by
- 3:57default in the result set it will
- 3:58display null value value but if you look
- 4:00at from the end user perspective null
- 4:03value doesn't make any sense at all
- 4:05instead of nulls don't you think it
- 4:07makes sense to display a word like this
- 4:09all or all cities which is more
- 4:12meaningful So when you say all here you
- 4:14know the user can read this as okay this
- 4:17total sales amount is for all cities in
- 4:21India country that's present in Asia
- 4:23continent whereas if you look at all
- 4:25here then this clearly says to the end
- 4:28user this is the grand total of total
- 4:29sales across all cities all countries
- 4:32and all continents okay so we can use
- 4:35this grouping function to achieve this
- 4:38now if you look at the result here
- 4:39notice that you know by whatever column
- 4:41we have the data aggregated across those
- 4:43columns we have null values now we want
- 4:47to replace those null values with this
- 4:50all word so let's see how to do
- 4:53that so first of all in our result set
- 4:56we don't need these three additional
- 4:58columns so I'm going to remove that
- 5:00and we want to use the grouping function
- 5:03so in order to replace the null value
- 5:06with this word all I'm going to use the
- 5:08case statement and grouping
- 5:11function so
- 5:14select
- 5:16case
- 5:20when grouping
- 5:22of let's take this continent pass it to
- 5:26our grouping function so when grouping
- 5:28of continent equals 1 that means data is
- 5:32aggregated across that column so if
- 5:35that's the case we know that we are
- 5:36going to get a null value and at that
- 5:39point we want to display all instead of
- 5:42null
- 5:44else so if it's not grouped across that
- 5:47continent then what do we want to
- 5:48display we want to display the
- 5:50respective continent name now if you
- 5:53look at the data that we have we don't
- 5:54have any null values within continent
- 5:57country or city columns but if if we
- 5:59have null values we don't want to
- 6:01display null instead we want to display
- 6:03you know unknown for either continent
- 6:06country or city so what I'm going to do
- 6:08here is use another isnull
- 6:11function and pass the continent column
- 6:14to it so if the continent column is null
- 6:18then I want to display you know this
- 6:21word unknown instead of null in the
- 6:24result set and then let's end the case
- 6:27statement as let's give the column and
- 6:30alas we want that column name to be
- 6:33continent
- 6:35okay so we need to do the same thing for
- 6:38country and city as well so I'm going to
- 6:40make a copy of that and let's pass
- 6:46country so grouping of country is equal
- 6:49to one then all else if country is null
- 6:53again display unknown and and we want
- 6:56the column name to be country and let's
- 6:58do the same thing
- 7:00for
- 7:02City so when grouping of city is equal
- 7:05to one then all else check if City
- 7:08column has got a null value if that's
- 7:10the case uh display unknown so by the
- 7:13way we have unknown spelling wrong here
- 7:15let's correct
- 7:19that and let's give this an alas of city
- 7:24and then we want
- 7:26the total sales amount all right so
- 7:30let's go ahead and execute this one now
- 7:33look at that instead of nulls we have
- 7:36all so now if you look at this one
- 7:37notice that this subtotal is for all
- 7:40cities all countries in Asia continent
- 7:43whereas this is a grand total it's for
- 7:45all cities all countries and all
- 7:47continents so much you know better than
- 7:51what we had
- 7:52before all right so here is that case
- 7:55statement that we have just looked at
- 7:57now the next obvious question that comes
- 7:59to our mind is why can't I use e null
- 8:03function instead of grouping function to
- 8:05achieve the same result well we can use
- 8:08it as long as we don't have any null
- 8:11data within the sales table let me
- 8:14explain what I mean so first of all
- 8:16let's see when we use eal function
- 8:18instead of you know grouping function if
- 8:20we get the same result I'm actually
- 8:21going to make a copy of this query so
- 8:24let's open another query editor
- 8:27window now let's let's use eal function
- 8:30so I'm going to get rid of this big case
- 8:33statement and let's use
- 8:35eal eal of
- 8:41continent so if continent is null then
- 8:45we want to display this word all let's
- 8:49do the same thing for country and
- 8:55city okay so if country is null then
- 9:00display all if city is null then display
- 9:03all so now when I execute this one
- 9:06notice that okay obviously we have to
- 9:07give that column an alas so let's give
- 9:10it an alas as continent and let's do the
- 9:13same thing for City and
- 9:19Country as
- 9:23country and let's do it for city as
- 9:28well so now when we execute this we get
- 9:32the same result right so when we use eal
- 9:36a grouping function we are able to
- 9:37achieve that
- 9:39now we can use e null function to
- 9:42achieve the same thing as long as our
- 9:45raw data in the sales table does not
- 9:47contain any null values let me explain
- 9:49what I mean so at the moment if you look
- 9:51at the sales table let's execute a
- 9:55select query on the sales
- 9:57table so this the table that we created
- 10:00in our previous video session so if you
- 10:02look at this continent country and city
- 10:04columns we don't have null values
- 10:06anywhere now let's go ahead and change
- 10:09you know the City Bangalore to null and
- 10:11to do that let's issue an update
- 10:13statement so
- 10:18update sales
- 10:20table set City
- 10:24equals null and the ID of that row is
- 10:27one so where ID equals 1 so now let me
- 10:31execute this and now when we select the
- 10:34data from the Sals table notice that for
- 10:36the first row for Asia continent India
- 10:38country you know cities now now when we
- 10:42execute this query look at what's going
- 10:44to happen since within the data itself
- 10:47we have null you know it's replacing
- 10:50that also with all and this is kind of
- 10:53misleading so you know if we read this
- 10:56from an end user perspective it's like
- 10:58for all cities within India country in
- 11:01Asia continent the total sales is one
- 11:04okay now why did we get all there that's
- 11:06because the raw data had a null value
- 11:08and because we are using e null function
- 11:10here wherever continent is null we
- 11:12replacing that with all and that's the
- 11:14reason why we got it there whereas when
- 11:17we use this
- 11:19query notice that we don't have that
- 11:21problem so when city is null it's
- 11:26displaying that as unknown we don't have
- 11:27a city name there so it's displaying
- 11:30unknown but not all okay so now this is
- 11:33much readable that's why grouping is
- 11:36very
- 11:39useful now this grouping function can be
- 11:42used with any of these functions rollup
- 11:44Cube or grouping sets so at the moment
- 11:47we using rollup when I use Cube and when
- 11:51we have the data aggregated across
- 11:52columns we are going to get you know the
- 11:55same result and when we use grouping
- 11:57sets as well so
- 11:59instead of cube when we
- 12:02use grouping sets again we can still
- 12:12use grouping function so this is the
- 12:16first group by which we want to group
- 12:18the total sale amount then we want it to
- 12:21group by continent country and just by
- 12:25continent finally
- 12:30grand total so here we using grouping
- 12:32sets so now when we execute this we
- 12:36still get
- 12:43all thank you for listening and have a
- 12:45great day
About this transcript
This page contains the full transcript of Grouping function in SQL Server by kudvenkat, generated from the public captions YouTube serves with the video. The transcript has 1,868 words across 274 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.