Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Order By Multiple Column + Case statement

Author  Topic 

aoriju
Posting Yak Master

156 Posts

Posted - 2010-04-29 : 09:37:56
ORDER BY
CASE WHEN @aint_OrderingId = 1
THEN Class_Code , Meal_Type ,Particular END


How to give multiple columns with in a case statement and order by clause

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-04-29 : 09:40:21
ORDER BY
CASE WHEN @aint_OrderingId = 1
THEN Class_Code + Meal_Type + Particular
ELSE 1 END
Go to Top of Page

aoriju
Posting Yak Master

156 Posts

Posted - 2010-04-29 : 09:43:18
Thanks for Your Quick replay
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-29 : 09:44:00
ORDER BY
CASE WHEN @aint_OrderingId = 1
THEN Class_Code END,
CASE WHEN @aint_OrderingId = 1
THEN Meal_Type END,
CASE WHEN @aint_OrderingId = 1
THEN Particular END



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

aoriju
Posting Yak Master

156 Posts

Posted - 2010-04-30 : 08:20:12
Dear ms65g,

It's working for Same sort of data Types
WHEN @aint_OrderingId =2
THEN MTRR.Meal_Type + MTRR.Class_Code + MTRR.Format
WHEN @aint_OrderingId = 3
THEN MTRR.Meal_Type + MTRR.Format + MTRR.Class_Code
ELSE M.Meal_Category + MTRR.sort_Order

In my case else part is not working because....M.Meal_Category is Varchar and MTRR.sort_Order is numeric
If i cast this sort order as varchar then ordering will lost(That means 1,11.00,2...work like that.....

Any solution please reply back
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-04-30 : 11:59:59
Try these two approaches:

ORDER BY (CASE WHEN @aint_OrderingId = 1 THEN Class_Code ELSE 1 END) ASC,
(CASE WHEN @aint_OrderingId = 1 THEN Meal_Type ELSE 1 END) ASC,
(CASE WHEN @aint_OrderingId = 1 THEN Particular ELSE 1 END) ASC,
(CASE WHEN @aint_OrderingId = 2 THEN MTRR.Meal_Type ELSE 1 END) ASC,
(CASE WHEN @aint_OrderingId = 2 THEN MTRR.Class_Code ELSE 1 END) ASC,
(CASE WHEN @aint_OrderingId = 2 THEN MTRR.Format ELSE 1 END)ASC,
(CASE WHEN @aint_OrderingId = 3 THEN MTRR.Meal_Type ELSE 1 END) ASC,
(CASE WHEN @aint_OrderingId = 3 THEN MTRR.Format ELSE 1 END) ASC,
(CASE WHEN @aint_OrderingId = 3 THEN MTRR.Class_Code ELSE 1 END) ASC,
(CASE WHEN @aint_OrderingId NOT IN (1, 2, 3) THEN M.Meal_Category ELSE 1 END) ASC,
(CASE WHEN @aint_OrderingId NOT IN (1, 2, 3) THEN MTRR.sort_Order ELSE 1 END) ASC;


ORDER BY
(CASE WHEN @aint_OrderingId = 1 THEN Class_Code + Meal_Type + Particular ELSE 1 END),
(CASE WHEN @aint_OrderingId = 2 THEN MTRR.Meal_Type + MTRR.Class_Code + MTRR.Format ELSE 1 END),
(CASE WHEN @aint_OrderingId = 3 THEN MTRR.Meal_Type + MTRR.Format + MTRR.Class_Code ELSE 1 END),
(CASE WHEN @aint_OrderingId NOT IN (1, 2, 3) THEN M.Meal_Category ELSE 1 END),
(CASE WHEN @aint_OrderingId NOT IN (1, 2, 3) THEN MTRR.sort_Order ELSE 1 END);
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-01 : 00:52:35
quote:
Originally posted by aoriju

Dear ms65g,

It's working for Same sort of data Types
WHEN @aint_OrderingId =2
THEN MTRR.Meal_Type + MTRR.Class_Code + MTRR.Format
WHEN @aint_OrderingId = 3
THEN MTRR.Meal_Type + MTRR.Format + MTRR.Class_Code
ELSE M.Meal_Category + MTRR.sort_Order

In my case else part is not working because....M.Meal_Category is Varchar and MTRR.sort_Order is numeric
If i cast this sort order as varchar then ordering will lost(That means 1,11.00,2...work like that.....

Any solution please reply back


i think based on your initial post it should be something like

CASE
WHEN @aint_OrderingId =2 OR @aint_OrderingId =3
THEN MTRR.Meal_Type
ELSE M.Meal_Category
END,
CASE
WHEN @aint_OrderingId =2
THEN MTRR.Class_Code
WHEN @aint_OrderingId = 3
THEN MTRR.Format
ELSE MTRR.sort_Order
END,
CASE
WHEN @aint_OrderingId =2
THEN MTRR.Format
WHEN @aint_OrderingId = 3
THEN MTRR.Class_Code
ELSE 1
END
[/code]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -