Monday, March 26, 2012

Proper Sorting of Months In DropdownList

I'm having an issue getting the data how I want it from SQL to then populate my dropdownlist.

I have a table called SALES_BUDGR_TBL that contains alot of columns but the ones I'm focusing on are SRCURM (Month) and SRACYR (Year). What I need to do is populate a dropdownlist and show the current month first and then work backwards. The data in the dabase goes back 14 months. I created a reference table called MonthOrder that has 3 columns, MonthID, MonthName and MonthAbrv. I did this so I have a way of saying which month belongs where when sorted.

Here is the select statement I currently have. I'm joining the tables and then I want to Order by the MonthID, but I would also like it to show the 2006 records first, then goto 2005, starting with December and going down of course.

Is there anyway to do this? My Select and Results are below...

SELECT DISTINCT SB.SRCURM, SB.SRACYR, MO.MonthID, MO.MonthName FROM SALES_BUDGR_TBL SB
INNER JOIN MonthOrder MO ON SB.SRCURM = MO.MonthAbrv
ORDER BY MO.MonthID DESC

DEC 2005 12 December
NOV 2005 11 November
OCT 2005 10 October
SEP 2005 09 September
AUG 2005 08 August
JUL 2005 07 July
JUN 2005 06 June
MAY 2005 05 May
APR 2005 04 April
APR 2006 04 April
MAR 2006 03 March
FEB 2005 02 Febuary
FEB 2006 02 Febuary
JAN 2006 01 January

If I say only get 2006 records it works but I need a better way to do this.

Please try to modify your ORDER clause to:

ORDER BY SB.SRACYR DESC, MO.MonthID DESC

|||

SELECT DISTINCT SB.SRACYR AS Year,DATEPART(month,SB.SRCURM+' 01, 2006') AS Month,CAST(SB.SRCURM+' 01, '+SB.SRACYR) AS MyDate FROM SALES_BUDGR_TBL SB
ORDER BY SB.SRACYR DESC,DATEPART(month,SB.SRCURM+' 01, 2006') DESC

|||Thanks, I got it working.

No comments:

Post a Comment