Solved

Sort Fiscal Months Desc by Name

Posted on 2008-09-29
5
669 Views
Last Modified: 2010-04-21
Hi -

I am trying to sort a report by fiscal month desc. Since our fiscal year begins in July and ends in June, the latest month for a year would be June sorted through July:

June
May
April
March

Or for the current date/year
September
August
July

I have found a way to do this whenever I have a fiscal month number or week number to work with, but for this particular table all I have is a fiscal month name, so sorting results in an alphabetical sort which I do not want. Is there a way to select the fiscal month as is by name, but select again to convert to a number so that I can sort it by the latest to the earliest? Or do I just have to add something behind the scenes (I prefer not to change the table) and like a case statement, and sort by a number assigned?

Thanks -
SD
0
Comment
Question by:savetheday
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 5

Expert Comment

by:NutrientMS
ID: 22600390
As far as I am aware sorting on a Month name will only do it alphabetically.  What I would do if you are able to modify the table is to add a colum for month number.  Where Month Date = January, month number = 1 etc... Then order it by Month Number.

I'm not sure if there is a way to incorporate something similar into your query.
0
 
LVL 5

Accepted Solution

by:
adlink_la earned 50 total points
ID: 22600539
Create a Date Dimension table.  Take a look at the table structure in the following article.  Im not saying you need a table with all of that stuff in it.  But if you use the same concept and create a table that has a row for every date and columns like FiscalMonthNumber, FiscalMonthName, FiscalQuarter, FiscalYear, etc., then you can increment the FiscalYear in July and set all of the other columns appropriately.  (One thing regarding the table structure in the article  I wouldnt use Date as a column name because it is a reserved word, maybe CalendarDate or whatever).

http://it.toolbox.com/wiki/index.php/Create_a_Time_Dimension_/_Date_Table


Using this table, you could sort dates for the year by doing something like:

SELECT CalendarDate
FROM Dates
WHERE FiscalYear = 2008
ORDER BY CalendarDate

Or you could join to your table with month names like:

SELECT d.CalendarDate, &
FROM Dates d
JOIN myTable a ON a.FiscalMonthName = d.FiscalMonthName
ORDER BY d.CalendarDate
0
 
LVL 5

Expert Comment

by:adlink_la
ID: 22600632
BTW, FiscalMonthName may not necessarily be the same as CalendarMonthName if the fiscal month does not start on the first of the month.  For example if the month always starts on the first Thursday or if there is some other obscure business rule, that is very easy to handle in a table.
0
 

Author Closing Comment

by:savetheday
ID: 31501341
Yes - joined the table on the other table to get the fiscal month number. Query runs slower but it works! Thanks!
SD
0
 
LVL 5

Expert Comment

by:adlink_la
ID: 22602084
You can probably speed it up by putting an index on the column you are joining on.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question