Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Grouping Sets

By Vaidya VS,

As per Question,

  1. As 1st Step, i created table in Sample Database (I used NORTHWIND) for the same.

use northwind

go

create table #T (Customer varchar(100),Year int,Sales money);

go

insert into #T (Customer, Year, Sales)values

('Jack', 2010, 10)

,('Jack', 2010, 15)

,('Bob', 2009, 5)

,('Dick',2011, 20);

go

       2. Then 2nd step, i queried for the records of #T (table) whether the 4 records have been successfully inserted.

use northwind

go

SELECT * FROM #T ORDER BY YEAR ASC

go

     3. After checking the above query, i further executed the following:-

use northwind

go

SELECT NULL as customer, year, SUM(sales)FROM #T GROUP BY year

UNION ALL

SELECT customer, NULL as year, SUM(sales)FROM #T GROUP BY customer;

go

    4. Further to that, i cross-checked from the following query with the above UNION ALL grouped query syntax :-

use northwind

go

SELECT customer, year, SUM(sales)FROM #T

GROUP BY GROUPING SETS ((customer), (year));

go

  • FINAL CONCLUSION: - RESULTSET's are matching exactly while running both queries.

Total article views: 862 | Views in the last 30 days: 3
 
Related Articles
FORUM

can't query on 'Order Details' table in Northwind

Northwind - Order Details

FORUM

Customize SQL Query

Customize

FORUM

customizing a query output

customizing a query output

FORUM

select query

select query

FORUM

Select query

Select query

Tags
grouping sets    
t-sql    
 
Contribute