Solved

Crosstab query sort order by months and years

Posted on 2008-06-20
14
1,659 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql statement - 9 25
Help with DoEvents 8 28
2 IIF's in Access query 25 31
Modify report 8 8
This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

810 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