|
When I ORDER BY inceptmonth my code works but when I use CASE to show the results on how I need them it shows the error:ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator. Can anyone advise how I can get around this? SELECT [Inceptmonth] ,SUM([policysIn])[policysIn] ,SUM([Late])[Late] ,AVG([avgdays])[avgdays] ,CONVERT(VARCHAR,CONVERT (decimal(5,0),SUM([ontime] * 100.0) / SUM([Policysin]))) AS [Success] ,DEPT FROM lee_em.dbo.[MONTHLYA] WHERE dept = 'cargo' GROUP BY INCEPTMONTH,DEPT UNION ALL SELECT 'Grand Total', SUM([policysIn]) ,SUM([Late]) ,AVG ([avgdays]) ,CONVERT(VARCHAR,CONVERT (DECIMAL(5,0),SUM([ontime] * 100.0) / SUM([Policysin]))) AS [Success] ,DEPT FROM lee_em.dbo.[MONTHLYA] WHERE dept = 'CARGO' ORDER BY CASE [Inceptmonth] WHEN 'Jan' then 1 WHEN 'Feb' then 2 WHEN 'Mar' then 3 WHEN 'Apr' then 4 WHEN 'May' then 5 WHEN 'Jun' then 6 WHEN 'Jul' then 7 WHEN 'Aug' then 8 WHEN 'Sep' then 9 WHEN 'Oct' then 10 WHEN 'Nov' then 11 WHEN 'Dec' then 12 END
(comments are locked)
|
|
I worked it out sorry for being hasty on asking this but think what I have done works. SELECT * FROM( SELECT [Inceptmonth] ,SUM([policysIn])[policysIn] ,SUM([Late])[Late] ,AVG([avgdays])[avgdays] ,CONVERT(VARCHAR,CONVERT (decimal(5,0),SUM([ontime] * 100.0) / SUM([Policysin]))) AS [Success] ,DEPT FROM lee_em.dbo.[MONTHLYA] WHERE dept = 'cargo' GROUP BY INCEPTMONTH,DEPT UNION ALL SELECT 'Grand Total', SUM([policysIn]) ,SUM([Late]) ,AVG ([avgdays]) ,CONVERT(VARCHAR,CONVERT (DECIMAL(5,0),SUM([ontime] * 100.0) / SUM([Policysin]))) AS [Success] ,DEPT FROM lee_em.dbo.[MONTHLYA] WHERE dept = 'CARGO' ORDER BY CASE [Inceptmonth] WHEN 'Jan' then 1 WHEN 'Feb' then 2 WHEN 'Mar' then 3 WHEN 'Apr' then 4 WHEN 'May' then 5 WHEN 'Jun' then 6 WHEN 'Jul' then 7 WHEN 'Aug' then 8 WHEN 'Sep' then 9 WHEN 'Oct' then 10 WHEN 'Nov' then 11 WHEN 'Dec' then 12 WHEN 'GRAND TOTAL' then 13 END
(comments are locked)
|
|
If you need some kind of mapping to get the data ordered in a certain way, I'd prefer the following query structure: Please note: what I've posted is a simplified version of your situation just to indicate how the query could be improved for readability/maintenance purposes.
(comments are locked)
|

