Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Friends,

I want to display the data in an order. Kindly check the below eg.
SQL
Select * Into #AP from (
Select Category='Computer',RATES_MASTER='50000'
union
Select Category='Computer',RATES_MASTER='20000'
union
Select Category='Computer',RATES_MASTER='15000'
union
Select Category='Laptop',RATES_MASTER='15000'
union
Select Category='Laptop',RATES_MASTER='40000'
union
Select Category='Laptop',RATES_MASTER='35000'
union
Select Category='Tab',RATES_MASTER='25000'
union
Select Category='Tab',RATES_MASTER='10000'
union
Select Category='Tab',RATES_MASTER='8000')o

Select * from #AP order by cast (RATES_MASTER as int) desc, category desc

Drop Table #AP

It give me the result as Desc. by Rates.
But actually I want to display the Material which cost is High and then group by Material.
Eg: Computer has high rate, so it should display as first category with all the rates, and then it should take the Laptop category.

Kindly help me on this, Thanks in advance.
Posted
Comments
gvprabu 19-Sep-13 10:06am    
Hi... I am checking... Some case my solution is giving different answer :-(
gvprabu 19-Sep-13 10:51am    
Hi... I updated the solution.. now it will work for all combinations

Hi,
my solution involves a temp table:

SQL
Select * Into #AP from (
Select Category='Computer',RATES_MASTER='50000'
union
Select Category='Computer',RATES_MASTER='20000'
union
Select Category='Computer',RATES_MASTER='15000'
union
Select Category='Laptop',RATES_MASTER='15000'
union
Select Category='Laptop',RATES_MASTER='40000'
union
Select Category='Laptop',RATES_MASTER='35000'
union
Select Category='Tab',RATES_MASTER='25000'
union
Select Category='Tab',RATES_MASTER='10000'
union
Select Category='Tab',RATES_MASTER='8000')o
 
CREATE TABLE #OrderdCats
(
Cat NVARCHAR(100) ,
max_rat int
);

insert into #OrderdCats
Select Category, MAX(cast(Rates_Master As int)) from #AP  Group by Category Order By MAX(cast(Rates_Master As int)) DESC;
 
Select a.* From #AP a, #OrderdCats o WHERE a.Category = o.Cat Order By o.max_rat desc, cast(a.Rates_Master As int) desc;

DROP TABLE #OrderdCats
Drop Table #AP
 
Share this answer
 
Comments
Arunprasath Natarajan 19-Sep-13 10:12am    
Tan q it helps and it was new for me.
gvprabu 19-Sep-13 10:27am    
Very nice Solution.... :-) my 5+
Maciej Los 20-Sep-13 9:05am    
5ed!
Hi,

Can you try like below Query...
SQL
Select * Into #AP from (
Select Category='Computer',RATES_MASTER='50000'
union
Select Category='Computer',RATES_MASTER='20000'
union
Select Category='Computer',RATES_MASTER='15000'
union
Select Category='Laptop',RATES_MASTER='15000'
union
Select Category='Laptop',RATES_MASTER='40000'
union
Select Category='Laptop',RATES_MASTER='35000'
union
Select Category='Tab',RATES_MASTER='25000'
union
Select Category='Tab',RATES_MASTER='10000'
union
Select Category='Tab',RATES_MASTER='8000')o
 
SELECT M.Category, M.RATES_MASTER
FROM #AP M
INNER JOIN (SELECT TOP 100 PERCENT T.Category, T.RATES_MASTER
			FROM (Select ROW_NUMBER()OVER (PARTITION BY Category ORDER BY CAST(RATES_MASTER AS INT) DESC) 'Rno', Category,RATES_MASTER 
			 FROM #AP ) T
			 WHERE T.Rno =1 
			 ORDER BY CAST(RATES_MASTER AS INT) DESC
			) S ON S.Category=M.Category 
ORDER BY CAST(S.RATES_MASTER AS INT) DESC, CAST(M.RATES_MASTER AS INT) DESC
 
Drop Table #AP

-- Note :I also used same like Solution 2, but with out 2nd Temp Table

Regards,
GVPrabu
 
Share this answer
 
v3
Comments
Arunprasath Natarajan 19-Sep-13 10:12am    
tan q it helps
Maciej Los 20-Sep-13 9:05am    
Good work!
+5
gvprabu 20-Sep-13 10:01am    
thanks my frnd....

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