Solved

Crosstab query sort order by months and years

Posted on 2008-06-20
14
1,663 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
[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
  • 9
  • 2
  • 2
  • +1
14 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 21833101
change it to:

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

Expert Comment

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

or just

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

Expert Comment

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

if multiple years involved.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

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
 
LVL 34

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 34

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 34

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 34

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 34

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

729 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