The GROUP BY CUBE statement is pretty sweet. Here’s an example.
First, let’s create our table and add data:
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N’[dbo].[GroupByCubeExample]‘)
AND type in (N’U'))
DROP TABLE [dbo].[GroupByCubeExample]
GO
CREATE TABLE [dbo].[GroupByCubeExample](
[Region] [nvarchar](50) NOT NULL,
[Product] [nvarchar](50) NOT NULL,
[Salesman] [nvarchar](50) NOT NULL,
[Amount] [money] NOT NULL
)
GO
INSERT INTO dbo.GroupByCubeExample
(Region,Product,Salesman,Amount)
SELECT ‘America’,'Bike’,'Bean, Adam’,10
UNION ALL SELECT ‘America’,'Bike’,'Stanford, Matt’,20
UNION [...]

Comments