[SQL Server 2005] Problem with ORDER BY clauseHello everyone, I hope your help.I have problem with order the output in this query.The output now is:MAT DVD SALESMAC L12 3MAE L12 3MAS L12 3Tot L12 9MAC L01 1Tot L01 5MAS L01 4
I need instead this other correct output:MAT DVD SALESMAE L12 3MAC L12 3MAS L12 3Tot L12 9MAC L01 1MAS L01 4Tot L01 5
Can you help me?Thank you in advance, your help is very appreciated.SELECT [MAT], [DVD], [SALES]FROM ( SELECT [MAT], [DVD], [SALES] FROM TestTable UNION SELECT COALESCE ([MAT], 'Tot') AS [MAT], [DVD], SUM ([SALES]) FROM TestTable GROUP BY (MAT), [DVD] ) qORDER BY [DVD] DESC, CASEWHEN MAT IS NULL THEN 1ELSE 0END;-- ------------------------------ Table structure for [dbo].[TestTable]-- ----------------------------DROP TABLE [dbo].[TestTable]GOCREATE TABLE [dbo].[TestTable] ([MAT] varchar(50) NULL ,[DVD] varchar(50) NULL ,[SALES] int NULL)GO-- ------------------------------ Records of TestTable-- ----------------------------INSERT INTO [dbo].[TestTable] ([MAT], [DVD], [SALES]) VALUES (N'Tot', N'L01', N'5');GOINSERT INTO [dbo].[TestTable] ([MAT], [DVD], [SALES]) VALUES (N'MAC', N'L12', N'3');GOINSERT INTO [dbo].[TestTable] ([MAT], [DVD], [SALES]) VALUES (N'MAS', N'L01', N'4');GOINSERT INTO [dbo].[TestTable] ([MAT], [DVD], [SALES]) VALUES (N'MAE', N'L12', N'3');GOINSERT INTO [dbo].[TestTable] ([MAT], [DVD], [SALES]) VALUES (N'MAC', N'L01', N'1');GOINSERT INTO [dbo].[TestTable] ([MAT], [DVD], [SALES]) VALUES (N'Tot', N'L12', N'9');GOINSERT INTO [dbo].[TestTable] ([MAT], [DVD], [SALES]) VALUES (N'MAS', N'L12', N'3');GO