Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

I have a table called Result and result table contains following columns.

Table -- Result

Columns--> ResultId, CategoryId, WeightFrom, PlayerName, Result

and user insert the record in this table like this.
ResultId | CategoryId | WeightFrom | PlayerName | Result
1          1           20.0          Ashu        Silver
2          1           20.0          Pawan       Gold
3          1           22            Mahesh      Bronze
4          1           23            Ram         Bronze
5          2           23.00         Rajesh      Bronze
6          2           22.00         Rakesh      Gold
7          2           20.00         Vikas       Silver
8          2           21            Suraj       Bronze

Now my question is I want to sort record using categoryId,WeightFrom,Result base

but main issue is that when i sort the record like this.

Select PlayerName,Result from Result order by CategoryId,WeightFrom, Result asc

Output is:

Ashu Silver
Pawan Gold
mahesh Bronze

But i want to sort record like this,

Pawan Gold
Ashu Silver
Mahesh Broze
Ram Bronze

I want to display first Gold, Silver, Bronze Record but category or weight from wise.

Please help me what i do for this.
Posted
Updated 11-Feb-15 21:05pm
v2
Comments
Tomas Takac 12-Feb-15 3:16am    
I'd say the output of your query as it is now should be Pawan, Ashu, Mahesh, Ram, Vikas, Suraj, Rakesh, Rajesh. Just because Pawan and Ashu have the same category and weight and Gold comes before Silver alphabetically. In category 2 the order is decide by weight. The problem is Result is a string and alphabetic order Bronze-Gold-Silver is not correct. But as i said this plays very little role in your current dataset. I'm not entirely sure how would you like the rows to be sorted. Can you provide full expected result?

This can be achieved through "CASE .. WHEN.." like following
SQL
ORDER BY CASE WHEN Result='Gold' THEN 1 WHEN Result='Silver' THEN 2 ELSE 3 END,CategoryId,WeightFrom


For example :
SQL
SELECT * FROM
(
	SELECT 1 ResultId,1 CategoryId,20.0 WeightFrom,'Ashu' PlayerName,'Silver' Result
	UNION
	SELECT 2,          1,           20.0,          'Pawan',       'Gold'
	UNION
	SELECT 3,          1,           22,            'Mahesh',      'Bronze'
	UNION
	SELECT 4,          1,           23,            'Ram',         'Bronze'
	UNION
	SELECT 5,          2,           23.00,         'Rajesh',      'Bronze'
	UNION
	SELECT 6,          2,           22.00,         'Rakesh',      'Gold'
	UNION
	SELECT 7,          2,           20.00,         'Vikas',       'Silver'
	UNION
	SELECT 8,          2,           21,            'Suraj',       'Bronze'
) AS T
ORDER BY CASE WHEN Result='Gold' THEN 1 WHEN Result='Silver' THEN 2 ELSE 3 END,CategoryId,WeightFrom

Result will be like:
ResultId	CategoryId	WeightFrom	PlayerName	Result
2	          1	           20.00	Pawan	      Gold
6	          2	           22.00	Rakesh	      Gold
1	          1	           20.00	Ashu	      Silver
7	          2	           20.00	Vikas	      Silver
3	          1	           22.00	Mahesh	      Bronze
4	          1	           23.00	Ram	      Bronze
8	          2	           21.00	Suraj	      Bronze
5	          2	           23.00	Rajesh	      Bronze

Now you can filter the data as per your requirement, may be using some ranking functions.
In case of any further assistance is required, please let me know :)
 
Share this answer
 
Comments
Ram Kumar(Webunitech) 15-Feb-15 10:20am    
Dear Sir thanks for your solutions but i need some change in this query i need gold, silver,brzone of same category order by like
1 gold
2 silver
3 broanze
plz help me
You can custom sort with CASE in your ORDER BY: http://sqlandme.com/2013/11/18/sql-server-custom-sorting-in-order-by-clause/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900