Solved

Crosstab query sort order by months and years

Posted on 2008-06-20
14
1,653 Views
Last Modified: 2013-11-28
Hi all,

I am attempting to create a dynamic graph utilizing MS Access that involks a crosstab query.  See below.
My problem is, check out the legend / order of the graph by dates.  It simply isn't in order except by alphabetical order.  Also, before this can of worms is opened, I am trying to take into consideration the year rolling aka - November 2007, December 2007, January 2008 in that order - NOT January 2008, November 2007, December 2007.
The first query which prompts for the date:
 

PARAMETERS [Enter the Start Date:] DateTime, [Enter the End Date:] DateTime;

SELECT Format([Date],"mmmm"", ""yyyy") AS CalculatedMonth, JobsPerHour.Area, [Jobs]/[Worked] AS JPH, Format([Date],"yyyymm") AS SortedMonth

FROM JobsPerHour

WHERE (((JobsPerHour.Date)>=[Enter the Start Date:] And (JobsPerHour.Date)<=[Enter the End Date:]))

ORDER BY Format([Date],"yyyymm");
 
 

The crosstab query, which calls on this first query:

TRANSFORM Avg(Query1.JPH) AS AvgOfJPH

SELECT Query1.Area

FROM Query1

GROUP BY Query1.Area

ORDER BY Query1.CalculatedMonth ASC

PIVOT Query1.CalculatedMonth;
 

and the report - see attachment

Open in new window

0
Comment
Question by:smagnus1
  • 9
  • 2
  • 2
  • +1
14 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 21833101
change it to:

Format([Date],"mm mmmm"", ""yyyy")
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 21833114
Format([Date],"mm mmmm yyyy")

or just

Format([Date],"mm yyyy")
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 21833121
Format([Date],"yyyy mm")

if multiple years involved.
0
 

Author Comment

by:smagnus1
ID: 21833310
Right sort order, but not a clean legend on the graph.  Any ideas?
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 21835565
If possible at all, please upload a minimized version of your mdb for me to work on it at home.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21836842
smagnus1,

Usually there is a way to manipulate the source data to display the chart tha way you want without the crosstab.

I too would like to see a sample dB

JeffCoachman
0
 

Author Comment

by:smagnus1
ID: 21837559
Fair enough.  My DB is attached.  For what it is worth, all data is randomized anyway, so nothing top secret here.  My report, oddly enough, is named "Report1".  This is an Access 2003 DB.  Thanks again.
ProductionRate.mdb
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 21837858
smagnus1Date,

First I will be able is monday morning. I whish I had this yesterday evening.

Mike
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 21839855
Change Query1 to:

PARAMETERS [Enter the Start Date:] DateTime, [Enter the End Date:] DateTime;
SELECT Format([Date]," yyyy"" Month: ""mm") AS CalculatedMonth, JobsPerHour.Area, [Jobs]/[Worked] AS JPH, Format([Date],"yyyymm") AS SortedMonth
FROM JobsPerHour
WHERE (((JobsPerHour.Date)>=[Enter the Start Date:] And (JobsPerHour.Date)<=[Enter the End Date:]))
ORDER BY Format([Date],"yyyymm");

If the attached display is acceptable.

Mike

untitled.bmp
0
 
LVL 33

Accepted Solution

by:
Mike Eghtebas earned 500 total points
ID: 21841314
Change Query1 to:

PARAMETERS [Enter the Start Date:] DateTime, [Enter the End Date:] DateTime;
SELECT Format([Date],"yyyy") & " " & Format([Date],"mm") & " (" & Format([Date],"mmm") & ")" AS CalculatedMonth, JobsPerHour.Area, [Jobs]/[Worked] AS JPH, Format([Date],"yyyymm") AS SortedMonth
FROM JobsPerHour
WHERE (((JobsPerHour.Date)>=[Enter the Start Date:] And (JobsPerHour.Date)<=[Enter the End Date:]))
ORDER BY Format([Date],"yyyymm");

If you prefer the following version instead.

Mike
Legend2.bmp
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 21842214
smagnus1,

Or...
Instead put the parenthesis around the Month Number (and not the Month  Name) to emphasize the Month Name instead of the month number.

SELECT Format([Date]," yyyy (mm) mmm") AS CalculatedMonth, JobsPerHour.Area, [Jobs]/[Worked] AS JPH, Format([Date],"yyyymm") AS SortedMonth
FROM JobsPerHour
ORDER BY Format([Date]," yyyy (mm) mmm"), Format([Date],"yyyymm");

<Or shorten the month number in brackets to  (m)
(one digit)
...to make the month number even more inconspicuous


But Points to eghtebas for coming up with this solution
;-)

Note: The issue here is that the crosstab query will "Sort" by the value.
So if the "Value" is 2008 Aug, that will be first.
If the "Value" is 2008 01, then that will be first.

So you must have the Value sorted by the Month Number in order to have it display in the chart in month order.
The Name of the month wiil have to be last.

JeffCoachman

untitled.JPG
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 21842309
smagnus1Date,

re:> Right sort order, but not a clean legend on the graph.  Any ideas?

Above this comment, you had your solution as:

Format([Date],"yyyy mm")

if multiple years involved.

Mike
0
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 21850824
smagnus1Date,

Any feedback?
0
 

Expert Comment

by:DataTransformationServices
ID: 35200623
Hello - this answer helped me alot.  I have one deviation from it.  In my Query1, I pull more that one years worth of data.  So I have monthly counts for two different years.  One of the years has data through to February.  The other year has data counts for the entire year.  The format commands works beautifullly if there are monthly counts for BOTH years only.  Isn't that strange?  Any ideas?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now