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)
Thank this author by sharing:
By Vaidya VS,
As per Question,
create table #T (Customer varchar(100),Year int,Sales money);
insert into #T (Customer, Year, Sales)values
('Jack', 2010, 10)
,('Jack', 2010, 15)
,('Bob', 2009, 5)
2. Then 2nd step, i queried for the records of #T (table) whether the 4 records have been successfully inserted.
SELECT * FROM #T ORDER BY YEAR ASC
3. After checking the above query, i further executed the following:-
SELECT NULL as customer, year, SUM(sales)FROM #T GROUP BY year
SELECT customer, NULL as year, SUM(sales)FROM #T GROUP BY customer;
4. Further to that, i cross-checked from the following query with the above UNION ALL grouped query syntax :-
SELECT customer, year, SUM(sales)FROM #T
GROUP BY GROUPING SETS ((customer), (year));
Northwind - Order Details
customizing a query output