web 2.0

GROUPING SETS in SQL SERVER 2008



Introduction

GROUPING SET were introduced in SQL Server 2008, which allow multiple groupings to be returned in one record set. In this article I explained the answers for the questions which raises to everybody’s mind who hear this for the first time.


What is GROUPING SETS?

GROUPING SETS were added in SQL Server 2008 to give you greater flexibility when defining SELECT statements that include aggregate functions. We can fetch multiple groups set by executing just one SQL query. Although there are other alternates to get multiple group sets with one SQL query for example by using ROLLUP and CUBE clauses. But GROUPING SETS provide more flexibility than other alternates.


T-SQL Syntax for GROUPING SETS

SELECT 
     Column1, 
     Column2, 
     COUNT(Column3) As Alias_Name 
FROM Table_Name 
GROUP BY GROUPING SETS 
     ( 
        (Column1), 
        (Column2) 
     ); 


Examples of GROUPING SETS

The examples in this section provide you with an idea of some of the options available when defining GROUPING SETS. I have taken different examples here to explain different scenarios.


Using GROUP BY with GROUPING SETS

USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
    ,S.Name AS N'Store', H.SalesPersonID
    ,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
    INNER JOIN Sales.Store S
        ON C.CustomerID  = S.CustomerID 
    INNER JOIN Sales.SalesTerritory T
        ON C.TerritoryID  = T.TerritoryID 
    INNER JOIN Sales.SalesOrderHeader H
        ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
    AND T.CountryRegionCode IN(N'DE', N'FR')
    AND H.SalesPersonID IN(284, 286, 289)
    AND SUBSTRING(S.Name,1,4)IN(N'Vers', N'Spa ')
GROUP BY GROUPING SETS
    (T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID)
ORDER BY T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID;

RESULT:


Using GROUPING SETS with composite elements

USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
    ,DATEPART(yyyy,OrderDate) AS 'Year'
    ,DATEPART(mm,OrderDate) AS 'Month'
    ,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
    INNER JOIN Sales.Store S
        ON C.CustomerID  = S.CustomerID 
    INNER JOIN Sales.SalesTerritory T
        ON C.TerritoryID  = T.TerritoryID 
    INNER JOIN Sales.SalesOrderHeader H
        ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
    AND T.CountryRegionCode IN(N'DE', N'FR')
    AND DATEPART(yyyy,OrderDate) = '2004'
GROUP BY GROUPING SETS(
    (T.[Group], T.CountryRegionCode)
    ,(DATEPART(yyyy,OrderDate), DATEPART(mm,OrderDate)))
ORDER BY T.[Group], T.CountryRegionCode
    ,DATEPART(yyyy,OrderDate), DATEPART(mm,OrderDate);

RESULT:


Using GROUP BY with multiple GROUPING SETS

USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
    ,S.Name AS N'Store', H.SalesPersonID
    ,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
    INNER JOIN Sales.Store S
        ON C.CustomerID  = S.CustomerID 
    INNER JOIN Sales.SalesTerritory T
        ON C.TerritoryID  = T.TerritoryID 
    INNER JOIN Sales.SalesOrderHeader H
        ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
    AND T.CountryRegionCode IN(N'DE', N'FR')
    AND H.SalesPersonID IN(284, 286, 289)
    AND SUBSTRING(S.Name,1,4)IN(N'Vers', N'Spa ')
GROUP BY GROUPING SETS(
    (T.[Group], T.CountryRegionCode)
    ,(S.Name)
    ,(H.SalesPersonID,T.[Group])
    ,(H.SalesPersonID)
    ,())
ORDER BY T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID;

RESULT:


Using GROUPING SETS with a ROLLUP of part of the GROUP BY list

USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
    ,S.Name AS N'Store', H.SalesPersonID
    ,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
    INNER JOIN Sales.Store S
        ON C.CustomerID  = S.CustomerID 
    INNER JOIN Sales.SalesTerritory T
        ON C.TerritoryID  = T.TerritoryID 
    INNER JOIN Sales.SalesOrderHeader H
        ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
    AND T.CountryRegionCode IN(N'DE', N'FR')
    AND H.SalesPersonID IN(284, 286, 289)
    AND SUBSTRING(S.Name,1,4)IN(N'Vers', N'Spa ')
GROUP BY GROUPING SETS(
    T.[Group], T.CountryRegionCode
   ,ROLLUP(S.Name, H.SalesPersonID))
ORDER BY T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID;

RESULT:


Using GROUPING SETS with a CUBE of part of the GROUP BY list

USE AdventureWorks;
GO
SELECT T.[Group] AS N'Region', T.CountryRegionCode AS N'Country'
    ,S.Name AS N'Store', H.SalesPersonID
    ,SUM(TotalDue) AS N'Total Sales'
FROM Sales.Customer C
    INNER JOIN Sales.Store S
        ON C.CustomerID  = S.CustomerID 
    INNER JOIN Sales.SalesTerritory T
        ON C.TerritoryID  = T.TerritoryID 
    INNER JOIN Sales.SalesOrderHeader H
        ON S.CustomerID = H.CustomerID
WHERE T.[Group] = N'Europe'
    AND T.CountryRegionCode IN(N'DE', N'FR')
    AND H.SalesPersonID IN(284, 286, 289)
    AND SUBSTRING(S.Name,1,4)IN(N'Vers', N'Spa ')
GROUP BY GROUPING SETS(
    T.[Group], T.CountryRegionCode
    ,CUBE(S.Name, H.SalesPersonID))
ORDER BY T.[Group], T.CountryRegionCode, S.Name, H.SalesPersonID;

RESULT:


GROUPING SETS Equivalents

A GROUP BY clause that uses GROUPING SETS can generate a result set equvalent to that generated by a UNION ALL of multiple simple GROUP BY clauses. GROUPING SETS can generate a result equivalent to that generated by a simple GROUP BY, ROLLUP or CUBE operation. Different combinations of GROUPING SETS, ROLLUP, or CUBE can generate equivalent result sets.


GROUPING SETS Equivalents of a Simple GROUP BY
GROUP BY GROUPING SETS ( (C1, C2, ..., Cn) )
GROUP BY C1, C2, ..., Cn
GROUPING SETS Equivalent of UNION ALL
SELECT customer, year, SUM(sales)
FROM T
GROUP BY GROUPING SETS ((customer), (year))
SELECT customer, NULL as year, SUM(sales)
FROM T 
GROUP BY customer
UNION ALL
SELECT NULL as customer, year, SUM(sales)
FROM T 
GROUP BY year
GROUPING SETS ROLLUP Equivalents
GROUP BY ROLLUP (C1, C2, …, Cn-1, Cn)
GROUP BY GROUPING SETS ( (C1, C2, …, Cn-1, Cn)
    ,(C1, C2, ..., Cn-1)
    ...
    ,(C1, C2)
    ,(C1)
    ,() )
GROUPING SETS CUBE Equivalents
GROUP BY CUBE (C1, C2, C3)
GROUP BY GROUPING SETS ( (C1, C2, C3)
    ,(C1, C2)
    ,(C1, C3)
    ,(C2, C3)
    ,(C1)
    ,(C2)
    ,(C3)
    ,() )

References

MSDN - SQL Server 2008

Tags: , , , ,

SQL Server

Fish Aquarium Game - feed the fish

Click anywhere in the tank below, to feed the fish:









Enjoy.....

Tags: ,

Game

Today's Most Viewed YouTube Videos




Full Match: IPL 2010 MATCH39 - KKR vs DD, 2010-04-07





IPL 2010 - DD vs KKR - Full Highlights





DMG Dil Mil Gaye 7th April 7 April 2010 Part 1 Dill Mill Gaye dmg





HIGH 15 - MATCH39 - IPL 2010 - KKR v DD





FUN FEED SNIPPETS - Interview - Shane Watson





Full Match: IPL 2010 MATCH38 - RR vs KXIP, 2010-04-07





Hot Tub Partay!! (4.6.10 - Day 341)





Pratigya - 7th April 2010 part 1





New Nike ad: Earl and Tiger Woods





World Premiere " UPLIFT OUR WOMEN " New Money Twinz





MJHT Miley Jab Hum Tum 7th April 7 April 2010 Part 1 Miley Jab Hum Tum mjht





Exclusive Onboard Video Of Ajith At Snetterton By STARAJITH





FUN FEED SNIPPETS - IPL Night





IPL 2010 - KXP vs RR - Yuvraj Singh's inning





Pavitra Rishta 7th April 7 Apr 2010 Part 1 Pavitra Rishta watch Full Parts On www.HIT2020.com





Taiwanese Boy Lin Yu Chun Sings Whitney Houston's "I Will Always Love You" LIVE





Pratigya - 7th April 2010 part 4 (last)





S Sreesanth to Michael Lumb





Stand-Up Comedy: Danny Minch on Fruits and Vegetables





HIGH 15 - MATCH38 - IPL 2010 - RR v KXIP





World's Fastest Nuke - An Epic 30 Seconds From GuN1t123 (MW2 Gameplay/Commentary)





IPL 2010 - KXP vs RR - 1st inning highlights





Uttaran 7th April 2010 part 1.wmv





Sasural Genda Phool 7th april 2010 Part 1 Sasural Genda Phool





IPL 2010 - KXP vs RR - Mahela Jayawardene's inning


Tags: , ,

General

Debugging AJAX using Javascript - Trick



How to review dynamic HTML content from browser using JavaScript?


These days everybody is using AJAX for web development. Sometimes we need to debug our AJAX code to review what actually get rendered in the browser. The problem is, if you view source code of the page, it will not display Dynamic HTML content.

I have a quick trick to get Dynamic HTML content from a rendered HTML page, directly from the Browser. This is very useful when you need to trace an issue on live website.

JAVASCRIPT: alert(document.body.innerHTML);

All you need to do is to copy the code written above and paste in the URL box of Browser where you website page is opened and press Enter. It will show whole document content(dynamic) in an alert.

To get the Dynamic HTML content on the page itself, use this code:

JAVASCRIPT: document.write(document.body.innerHTML);

You can write your own JavaScript logic and run this way to get result according to your requirement.

All the Best!!

Tags:

JavaScript

Data Type Synonyms

Introduction

This article basically illustrates the concept of Data type synonyms which are included in SQL Server 2005 for SQL-92 compatibility.

What are Data Type Synonyms?

Data type synonyms can be used instead of the corresponding base data type name in data definition language (DDL) statements, such as CREATE TABLE, CREATE PROCEDURE, or DECLARE @variable. However, after the object is created, the synonyms have no visibility. When the object is created, the object is assigned the base data type that is associated with the synonym. There is no record that the synonym was specified in the statement that created the object.
All objects that are derived from the original object, such as result set columns or expressions, are assigned the base data type. All subsequent metadata functions performed on the original object and any derived objects will report the base data type, not the synonym. This behavior occurs with metadata operations, such as sp_help and other system stored procedures, the information schema views, or the various data access API metadata operations that report the data types of table or result set columns.

List of Data Type Synonyms  

Synonym SQL Server system data type
Binary varying varbinary
char varying  varchar
character char
character char(1)
character(n) char(n)
character varying(n) varchar(n)
Dec decimal
Double precision float
float[(n)] for n = 1-7 real
float[(n)] for n = 8-15 float
integer int
national character( n ) nchar(n)
national char( n ) nchar(n)
national character varying( n ) nvarchar(n)
national char varying( n ) nvarchar(n)
national text ntext
rowversion timestamp

How to use Data Type Synonyms 

For example, you can create a table by specifying Data Type Synonyms:

 	CREATE TABLE Student(
	ID integer PRIMARY KEY,
	FirstName character varying(20),
	LastName national character varying(20),
	Marks Double precision)

FirstName is actually assigned an varchar(20) data type, and all subsequent metadata functions will report the column as an varchar(20) column. The metadata functions will never report them as a character varying(20) column. Similarly, LastName is an nvarchar(20) data type column and Makes is a float column.

Conclusion

This is an introduction to the Data Type Synonyms in SQL Server. The idea is to get familiar with another way of writing DDL queries using Data Type Synonyms.

Reference

MSDN

Tags:

SQL Server