Click here to Skip to main content
15,920,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear sir,

hear is my data and i want to select every month top 4 record
hear is sep month have 8 record only select top 4 record of every month .

FreightRate DwtDate  ZoneFrom	ZoneTo	Dwt
0.00	22 Nov 2011	19	14	1
89.00	22 Nov 2011	19	14	2
70.00	22 Nov 2011	19	14	5
49.00	22 Nov 2011	19	14	10
0.00	28 Sep 2011	19	14	1
87.00	28 Sep 2011	19	14	2
68.00	28 Sep 2011	19	14	5
48.00	28 Sep 2011	19	14	10
0.00	06 Sep 2011	19	14	1
89.00	06 Sep 2011	19	14	2
69.00	06 Sep 2011	19	14	5
49.00	06 Sep 2011	19	14	10
0.00	20 Jun 2011	19	14	1
93.00	20 Jun 2011	19	14	2
70.00	20 Jun 2011	19	14	5
51.00	20 Jun 2011	19	14	10
90.00	10 May 2011	19	14	2
64.00	10 May 2011	19	14	5
48.00	10 May 2011	19	14	10
91.00	10 Apr 2011	19	14	2


thanks in advance .
Posted
Updated 6-Dec-11 19:01pm
v2
Comments
[no name] 7-Dec-11 0:46am    
not clear

Hi Dinesh,
Here it is :

SQL
select * from (
SELECT *, DATEPART(MONTH, dwtdate) month  , rank() over (partition by DATEPART(MONTH, dwtdate) order by freightrate desc) Ranking
FROM Freight
) marshaled where Ranking <= 4
order by month, ranking


To read more about ranking functions :
http://msdn.microsoft.com/en-us/library/ms189798.aspx[^]


And remember that I supposed that all of your data is at the same year. So consider to change it and adapt it to your needs.
Also I named your table Freight so remember to change it too.

Good Luck
 
Share this answer
 
Comments
[no name] 7-Dec-11 2:26am    
good :) my 5+
Amir Mahfoozi 7-Dec-11 2:27am    
Thank you Maulik
try this code:


SQL
select TOP 4 * from MonthTable where dwtDate LIKE %+ 'SEP' +%


hope it helps,


thnks
 
Share this answer
 
Comments
DINESH K MAURYA 7-Dec-11 1:00am    
i want to select every month such as nov, sep, jun, may
D K N T H 7-Dec-11 1:01am    
in just 1 query and result?

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