aspx Tutorial

.NET Articles,jQuery demo, asp.net with jQuery, online tutorial,Jquery, SilverLight, Javascript, asp.net,JSON, MVC,.NET Articles,demo, Web Services, .NET articles, Sharepoint 2010, visual studio 2010,Aamir Hasan,IT
Advertise Here

Advertize

wwwSW
Posted by Aamir Hasan   on Saturday, September 11, 2010 Total Views:  

Set of groups by varying columns is called Grouping Sets. Grouping sets are equivalent to a Union All of quires.Microsoft introduce Grouping Sets in MS SQL 2008, it is introduce for developer to work easily.
you can also use Union All if you not have MS SQL 2008 or later versions.

 

Syntax

Group by Grouping sets  (col1,col2,col3,.....,coln)


Let's Start. Open your MS SQL server 2008. Create a new database or select existing database where you want to work. In this Example, i am using existing database.I have created temporary virtual tables with declare statement. A temporary virtual table is a table which look like a table but not store information in a database. Virtual table are read only.Virtual table are destroy automatically when the query execution is stop. Virtual table can not use syntax Drop.

Create new temporary virtual tables  named Patient and PatientVisits  as shown in Figure 1.1

Figure 1.1

DECLARE @PATIENT TABLE
(
ID BIGINT ,
FULLNAME NVARCHAR(MAX) ,
DOB DATE ,
GENDER CHAR(1)
)
DECLARE @PATIENTVISITS TABLE
(
PATIENT_ID BIGINT ,
VISIT_DATE DATE ,
VISIT_TIME TIME ,
DOCTOR_FEE MONEY
)

Let's store some records in Patient and PatientVisits temporary virtual tables as  shown in Figure 1.2

Figure 1.2

 

INSERT  INTO @PATIENT
        ( ID, FULLNAME, DOB, GENDER )
VALUES  ( 1, 'AAMIR HASAN', '08/13/1985', 'M' ),
        ( 2, 'AWAIS AHMED', '01/05/1965', 'M' ),
        ( 3, 'HASAN', '02/23/1980', 'M' ),
        ( 4, 'MAYRA GIBRAM', '08/13/1975', 'F' ),
        ( 5, 'SABA KHAN', '02/13/1995', 'F' ),
        ( 6, 'AAMIR HASAN', '01/01/1985', 'M' ),
        ( 7, 'MAHWISH HASAN', '04/17/1988', 'F' ),
        ( 8, 'SANA KHAN', '01/11/1981', 'F' ),
        ( 9, 'ZAHOOR AHMED', '08/13/1949', 'M' )
     
     
INSERT  INTO @PATIENTVISITS
        ( PATIENT_ID, VISIT_DATE, VISIT_TIME, DOCTOR_FEE )
VALUES  ( 1, '12/12/1981', '12:09:09', 500 ),
        ( 2, '01/12/1982', '12:09:09', 400 ),
        ( 3, '11/12/1982', '12:09:09', 300 ),
        ( 4, '11/12/1982', '12:09:09', 500 ),
        ( 2, '11/12/1982', '12:09:09', 800 ),
        ( 5, '11/11/2000', '12:09:09', 900 ),
        ( 6, '11/11/2003', '12:09:09', 500 ),
        ( 2, '11/11/2003', '12:09:09', 100 ),
        ( 3, '11/11/2003', '12:09:09', 500 ),
        ( 6, '11/12/2003', '12:09:09', 500 ),
        ( 7, '11/12/2003', '12:09:09', 500 ),
        ( 8, '11/12/2004', '12:09:09', 500 ),
        ( 1, '01/09/2005', '12:09:09', 500 ),
        ( 2, '05/09/2006', '12:09:09', 1500 ),
        ( 9, '05/09/2007', '12:09:09', 900 ),
        ( 1, '05/09/2008', '12:09:09', 100 ),
        ( 2, '05/09/2009', '12:09:09', 500 ),
        ( 4, '05/12/2009', '12:09:09', 400 ),
        ( 7, '05/12/2009', '12:09:09', 500 ),
        ( 8, '05/12/2009', '12:09:09', 200 ),
        ( 9, '09/12/2009', '12:09:09', 500 ),
        ( 2, '01/01/2010', '12:09:09', 5500 )

 

 

Following query will show the patient and patientVisits temporary virtual tables.

SELECT  *
FROM    @PATIENT
SELECT  *
FROM    @PATIENTVISITS

Note: This should be done in one execution because above tables are virtual tables.

 

Following  query displays the Patient ID, patient Name, total sum of each patient of all visits.

SELECT  ID,fullname,SUM(DOCTOR_FEE)
FROM    @PATIENT P ,
       @PATIENTVISITS PV
WHERE   P.ID = PV.PATIENT_ID
GROUP BY GROUPING SETS((ID,fullname))

Output

 

Following query will returns All Patient ID, Patient Name, Doctor fee and grand total of All patients.

SELECT  ID,fullname,SUM(DOCTOR_FEE)
FROM    @PATIENT P ,
       @PATIENTVISITS PV
WHERE   P.ID = PV.PATIENT_ID
GROUP BY GROUPING SETS((ID,fullname),VISIT_DATE )

Output

 

Following query will returns year wise doctor fee.

SELECT  VISIT_DATE ,
       SUM(DOCTOR_FEE)
FROM    @PATIENT P ,
       @PATIENTVISITS PV
WHERE   P.ID = PV.PATIENT_ID
GROUP BY GROUPING SETS(( VISIT_DATE ))

Output


Following query shows the year wise patient id,Patient name, year and total of doctor fee of all patients at the end of each year and grand total at the end of row.

SELECT  id ,
       fullname ,
       YEAR(visit_date) ,
       SUM(DOCTOR_FEE)
FROM    @PATIENT P ,
       @PATIENTVISITS PV
WHERE   P.ID = PV.PATIENT_ID
GROUP BY GROUPING SETS(( id ,
                        fullname ,
                        YEAR(visit_date)
                      ), id, ( ))

Output

Following query will returns each patient records yearly group wise and total of doctor fee of each patient at the end of year.

SELECT  id ,
       fullname ,
       YEAR(visit_date) AS [visit date] ,
       SUM(DOCTOR_FEE) AS [Fee]
FROM    @PATIENT P ,
       @PATIENTVISITS PV
WHERE   P.ID = PV.PATIENT_ID
GROUP BY GROUPING SETS(( id ,
                        fullname ,
                        YEAR(visit_date)
                      ), ( id ), ( YEAR(visit_date) ), ( ))

Output

Following query will return each month of year group of each patient records and total fee of doctor at each month and grand total of doctor fee at the end of row.

SELECT  id ,
       fullname ,
       MONTH(visit_date) AS [MONTH] ,
       YEAR(visit_date) AS [Year] ,
       SUM(DOCTOR_FEE) AS [Fee]
FROM    @PATIENT P ,
       @PATIENTVISITS PV
WHERE   P.ID = PV.PATIENT_ID
GROUP BY GROUPING SETS(( id ,
                        fullname ,
                        ROLLUP(YEAR(visit_date), MONTH(visit_date))
                      ))

Output

Conclusion

Grouing sets is avaible in ms sql 2008, before going to start grouping set, you must have knowledge of grouping sets.

Download

Case 1 (1.92 kb)

Case_2.sql (2.04 kb)

Case_3.sql (2.07 kb)

Case_4.sql (2.05 kb)

Case_5.sql (2.19 kb)

Case_6.sql (2.26 kb)

Case_7.sql (2.29 kb)

Protected by Copyscape Online Plagiarism Tool

Comments (40) -

plr article content
plr article content United States
10/11/2010 7:33:39 AM #

Good post. I will post this into my twitter account.

easy dinner recipes for two
easy dinner recipes for two United States
10/11/2010 12:00:06 PM #

Do you have any more info on this?

wow gold
wow gold United States
10/15/2010 3:58:38 AM #

Such a usefule blog wow !!!!

chloe handbags
chloe handbags United States
10/29/2010 4:37:28 AM #

clothing and luxurious accessories handbags money could buy If you

replica movado
replica movado United States
11/3/2010 3:17:58 PM #

producing high technological products Things from  Searching for

ffxiv power leveling
ffxiv power leveling United States
11/8/2010 1:06:06 PM #

of games back then view of the gil Crystal Chronicles at the

replica louis vuitton handbags
replica louis vuitton handbags United States
11/8/2010 1:22:19 PM #

stay on the cutting edge of fashion purses cost you $500 or more It is

marc jacobs
marc jacobs United States
11/8/2010 4:09:16 PM #

classic Loewe monogram whether they prefer over the

online catalogue shopping
online catalogue shopping United States
11/9/2010 1:48:16 PM #

this is why I read your blog

fake louis vuitton handbags
fake louis vuitton handbags United States
11/9/2010 2:34:24 PM #

the market Handbag price ranges on your Gucci handbag dont worry

dog grooming
dog grooming United States
11/14/2010 7:51:54 AM #

THIS is why I keep reading your blog, excellent post.

mulberry
mulberry United States
11/14/2010 5:01:19 PM #

several days or one small enough black hobo so darn expensive It

louis vuitton handbags
louis vuitton handbags United States
11/14/2010 5:57:53 PM #

create an added style This gucci around and make sure that you are

oris watches
oris watches United States
11/15/2010 7:07:13 AM #

arent just for men With feminine styling to The best source for replica

project management course
project management course United States
11/15/2010 3:00:33 PM #

Your theme is broken, just letting you know.

ab belt
ab belt United States
11/17/2010 6:24:51 AM #

That's what she said!

replica patek philippe
replica patek philippe United States
11/21/2010 10:46:45 PM #

are valuable And their values can be increased as innovated every year but the concepts which can

breitling watches
breitling watches United States
11/22/2010 3:47:48 PM #

collections Patrimony Grandes Complications ladies diamond watch for Hungarian Countess

replica montblanc watches
replica montblanc watches United States
11/25/2010 12:18:53 PM #

but in terms of quality they pale in comparison beauty of the passage of time minutes and seconds

hublot watches
hublot watches United States
11/28/2010 1:38:16 PM #

are planning to issue their own concept editions good idea as some of the replica watches are

fertility acupuncture newtown
fertility acupuncture newtown United States
11/28/2010 7:49:23 PM #

Just shared this on facebook, keep it up!

Halloween costumes UK
Halloween costumes UK United States
12/1/2010 8:25:09 PM #

Well, thats news to me. Thanks for the info.

portable cell phone jammer
portable cell phone jammer United States
12/2/2010 8:30:01 PM #

Hey brah, your RSS feed is broken.

forex trading
forex trading United States
12/28/2010 10:38:03 AM #

Is that it? Details please!

no fax cash loan
no fax cash loan United States
1/13/2011 9:12:02 AM #

Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.

retouche de photos
retouche de photos United States
2/10/2011 8:01:05 PM #


I wanted to thank you for this excellent read!! I definitely loved every little bit of it.Cheers for the info!!!! & This is the perfect blog for anyone who wants to know about this topic. You know so much its almost hard to argue with you .........
thanks

payday loans direct lender
payday loans direct lender United Kingdom
4/10/2011 11:30:22 PM #

Really nice blog, very informative. Thanks dude for wonderful posting. Keep it up in the future as well.

wind power systems
wind power systems Australia
5/31/2011 7:33:50 PM #

Really fascinating subject, I will bookmark your website to check if you write more in the future.

Solar Street Light
Solar Street Light Islamic Republic of Pakistan
6/8/2011 7:52:14 PM #

very nice post,thanks for share very helpful post!

Solar Hot Water System
Solar Hot Water System Australia
7/4/2011 2:01:32 AM #

Fabulous post.nice blog and ideas

corporate logo design
corporate logo design United Kingdom
7/10/2011 9:16:48 PM #

I really like it.

Solar hot water systems
Solar hot water systems Australia
7/12/2011 8:08:04 PM #

Solar energy systems are environmentally friendly, that is the biggest reason to choose them.

Play Bingo
Play Bingo United States
7/26/2011 8:50:18 PM #

Thanks a lot for posting such interesting material! I love your website, it's is very unique!

Antrel Rolle Jersey
Antrel Rolle Jersey United States
10/5/2011 12:37:52 AM #

I was reading something else about this on another blog.
Interesting. Your position on it is diametrically contradicted to what I read earlier. I am still contemplating over the opposite points of view, but I’m tipped heavily toward yours. And no matter, that’s what is so great about modernized democracy and the marketplace of thoughts on-line.

Milwaukee carpet cleaners
Milwaukee carpet cleaners United States
10/5/2011 10:06:14 PM #

Interesting post, thanks for the useful information.

Cheap logo design
Cheap logo design United Kingdom
10/6/2011 3:16:55 AM #

Wow. You can listen to the track right here, and happy divine thoughts about the universe everybody!

penis enlargement
penis enlargement United States
10/23/2011 8:39:27 PM #

great post.. i really enjoyed it

penis enlargement
penis enlargement United States
10/24/2011 4:48:42 AM #

great post.. i really enjoyed it

china wholesale
china wholesale People's Republic of China
11/22/2011 9:19:55 PM #


Hi, the article is so wonderful, I am interested in it. I will pay attention to your articles. And I like <a href="http://www.withgoodsale.com/">cheap clothes</a> very much,they are in high quality  and inexpensive, so I think everyone will be interested in it,is it right? I have found a <a href="http://www.withgoodsale.com/">wholesale in china</a> online, there are China wholesaler.The  store was established for a long time.They have a good reputation,there are many <a href="http://www.withgoodsale.com/">cheap shoes</a> on their website.They provide wholesale price  and shipping to worldwide. So no matter where you are now, you also can order your goods at this  website. Especially the <a href="http://www.withgoodsale.com/">cheap clothing</a> , they are my favorite!

Linux Hosting
Linux Hosting United States
12/19/2011 9:38:17 PM #

Nice information is provided through this blog and it is nice to visit this blog and the information provided here. It is an educational blog which increase our information and news.

solar power systems
solar power systems Islamic Republic of Pakistan
1/9/2012 1:39:31 AM #

Thanks for providing such useful information. I really appreciate your professional approach. I would like to thank you for the efforts you made in writing this post. I am hoping the same best work from you in the future as well.
www.greenbiz.com.pk/solar_pv_panel_pk_res.php

Wind Turbines
Wind Turbines Islamic Republic of Pakistan
2/6/2012 2:13:10 AM #

Just read your post and would like to thank you for maintaining such a cool blog.
http://www.greenbiz.com.pk/wind-energy.php

fafafa
fafafa Slovenia
4/23/2012 3:05:26 AM #

http://www.christianlouboutinukk.org   christian louboutin
http://www.tiffanyuks.org   tiffany uk
http://www.gucciukbeltuk.org   gucci belt
http://www.frsaclouisvuittonsac.com   louis vuitton sac
http://www.chanelukoutletuks.org   chanel outlet
http://www.burberryukoutletuk.org   burberry
http://www.poloralphlaurenuko.org   ralph lauren uk

Kings Blank Jersey
Kings Blank Jersey Nepal
5/17/2012 4:15:58 PM #

http://www.shoplosangeleskings.com           kings jerseys
www.shoplosangeleskings.com/anze-kopitar-jersey           Anze Kopitar Jersey
www.shoplosangeleskings.com/drew-doughty-jersey           Drew Doughty Jersey
www.shoplosangeleskings.com/dustin-brown-jersey           Dustin Brown Jersey
www.shoplosangeleskings.com/jonathan-quick-jersey           Jonathan Quick Jersey
www.shoplosangeleskings.com/kings-blank-jersey           Kings Blank Jersey
www.shoplosangeleskings.com/martin-mcsorley-jersey           Martin McSorley Jersey
www.shoplosangeleskings.com/mike-richards-jersey           Mike Richards Jersey
www.shoplosangeleskings.com/ryan-smyth-jersey           Ryan Smyth Jersey
www.shoplosangeleskings.com/wayne-gretzky-jersey           Wayne Gretzky Jersey

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5