?
Solved

How sort query results based on a formatted date field

Posted on 2014-01-24
8
Medium Priority
?
453 Views
Last Modified: 2014-01-29
I have a query which is the record source for a report.  The query has a field in it that is this:

DateWorked2: Format([DateWorked],"mmm")

So if the DateWorked is 10/15/13 then the result in the query is Oct.

But I have to apply a sort in the report using the date field but the year has to go from July through Jun.

So in the report the detail has to be sorted by this sort criteria  The report will look like:

July..... data, data, data, etc.
August..... data, data, data, etc.
September..... data, data, data, etc.
October..... data, data, data, etc.
November..... data, data, data, etc.
December..... data, data, data, etc.
January..... data, data, data, etc.
February..... data, data, data, etc.
Match..... data, data, data, etc.
April..... data, data, data, etc.
May..... data, data, data, etc.
June..... data, data, data, etc.

How can I do this?
0
Comment
Question by:SteveL13
[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
  • 4
  • 3
8 Comments
 
LVL 15

Expert Comment

by:OMC2000
ID: 39806828
You could sort records by actual values of DateWorked and determine your Year using dateadd function moving DateWorked month to the past:

SELECT testdates.[DateWorked], testdates.[DateWorked], Format([DateWorked],"mmm"), Format(DateAdd("M", -6, [DateWorked]),"yyyy") myyear
FROM testdates order by  [DateWorked] ;

them the first month in your year will be July
0
 

Author Comment

by:SteveL13
ID: 39806843
Sorry I don't understand.  Do I need to add a column to the query?  How would I set the column up in the query designer?
0
 
LVL 15

Expert Comment

by:OMC2000
ID: 39806858
Yes, you need column, which should display value of the year for a record.
I don't use query designer, and don't know how it works. You could switch to SQL mode and edit a query
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:SteveL13
ID: 39806874
I am lost in SQL.  Sorry again.

Here is the SQL.  What do I do with it?

SELECT Format([DateWorked],"mmm") AS DateWorked2, Sum(Nz([TouringProgramCount1],0)+Nz([TouringProgramCount2],0)+Nz([TouringProgramCount3],0)) AS TouringProgramCount, Sum(Nz([TouringGuestSum1],0)+Nz([TouringGuestSum2],0)+Nz([TouringGuestSum3])) AS TouringProgramGuestSum, Sum(Nz([JustAskProgramCount1],0)+Nz([JustAskProgramCount2],0)+Nz([JustAskProgramCount3],0)) AS JustAskProgramCount, Sum(Nz([JustAskGuestSum1],0)+Nz([JustAskGuestSum2],0)+Nz([JustAskGuestSum3],0)) AS JustAskGuestSum, Sum(Nz([JustAskProgramCount1],0)+Nz([JustAskProgramCount2],0)+Nz([JustAskProgramCount3],0)) AS HouseGuidesProgramCount, Sum(Nz([HouseGuidesGuestSum1],0)+Nz([HouseGuidesGuestSum2],0)+Nz([HouseGuidesGuestSum3],0)) AS HouseGuidesGuestSum
FROM qryDocentCommitteeActivityStep1
GROUP BY Format([DateWorked],"mmm");
0
 
LVL 15

Expert Comment

by:OMC2000
ID: 39806939
I think it should be like the following:

SELECT Format([DateWorked],"mmm") AS DateWorked2, Sum(Nz([TouringProgramCount1],0)+Nz([TouringProgramCount2],0)+Nz([TouringProgramCount3],0)) AS TouringProgramCount, Sum(Nz([TouringGuestSum1],0)+Nz([TouringGuestSum2],0)+Nz([TouringGuestSum3])) AS TouringProgramGuestSum, Sum(Nz([JustAskProgramCount1],0)+Nz([JustAskProgramCount2],0)+Nz([JustAskProgramCount3],0)) AS JustAskProgramCount, Sum(Nz([JustAskGuestSum1],0)+Nz([JustAskGuestSum2],0)+Nz([JustAskGuestSum3],0)) AS JustAskGuestSum, Sum(Nz([JustAskProgramCount1],0)+Nz([JustAskProgramCount2],0)+Nz([JustAskProgramCount3],0)) AS HouseGuidesProgramCount, Sum(Nz([HouseGuidesGuestSum1],0)+Nz([HouseGuidesGuestSum2],0)+Nz([HouseGuidesGuestSum3],0)) AS HouseGuidesGuestSum, Format(DateAdd("M", -6, [DateWorked]),"yyyy") AS myyear
FROM qryDocentCommitteeActivityStep1
GROUP BY Format(DateAdd("M", -6, [DateWorked]),"yyyy"), Format([DateWorked],"mmm");
0
 

Author Comment

by:SteveL13
ID: 39807050
Really strange.  Using the original code I gave you and your suggested code I get the same results.  And the months are in the wrong order.  Please see attached file.
Month-Order.jpg
0
 
LVL 41

Expert Comment

by:Sharath
ID: 39807931
try this.
 
SELECT FORMAT([DateWorked], "mmm")                    AS DateWorked2, 
       SUM(NZ([TouringProgramCount1], 0) 
           + NZ([TouringProgramCount2], 0) 
           + NZ([TouringProgramCount3], 0))           AS TouringProgramCount, 
       SUM(NZ([TouringGuestSum1], 0) 
           + NZ([TouringGuestSum2], 0) 
           + NZ([TouringGuestSum3]))                  AS TouringProgramGuestSum, 
       SUM(NZ([JustAskProgramCount1], 0) 
           + NZ([JustAskProgramCount2], 0) 
           + NZ([JustAskProgramCount3], 0))           AS JustAskProgramCount, 
       SUM(NZ([JustAskGuestSum1], 0) 
           + NZ([JustAskGuestSum2], 0) 
           + NZ([JustAskGuestSum3], 0))               AS JustAskGuestSum, 
       SUM(NZ([JustAskProgramCount1], 0) 
           + NZ([JustAskProgramCount2], 0) 
           + NZ([JustAskProgramCount3], 0))           AS HouseGuidesProgramCount, 
       SUM(NZ([HouseGuidesGuestSum1], 0) 
           + NZ([HouseGuidesGuestSum2], 0) 
           + NZ([HouseGuidesGuestSum3], 0))           AS HouseGuidesGuestSum, 
       FORMAT(DATEADD("M", -6, [DateWorked]), "yyyy") AS myyear 
  FROM qryDocentCommitteeActivityStep1 
 GROUP BY FORMAT([DateWorked], "mmm"), 
          MONTH([DateWorked]) 
 ORDER BY IIF(MONTH([DateWorked]) < 7, MONTH([DateWorked]) + 12, MONTH([DateWorked]));

Open in new window

0
 
LVL 15

Accepted Solution

by:
OMC2000 earned 2000 total points
ID: 39808301
I missed order by clause


SELECT Format([DateWorked],"mmm") AS DateWorked2, Sum(Nz([TouringProgramCount1],0)+Nz([TouringProgramCount2],0)+Nz([TouringProgramCount3],0)) AS TouringProgramCount, Sum(Nz([TouringGuestSum1],0)+Nz([TouringGuestSum2],0)+Nz([TouringGuestSum3])) AS TouringProgramGuestSum, Sum(Nz([JustAskProgramCount1],0)+Nz([JustAskProgramCount2],0)+Nz([JustAskProgramCount3],0)) AS JustAskProgramCount, Sum(Nz([JustAskGuestSum1],0)+Nz([JustAskGuestSum2],0)+Nz([JustAskGuestSum3],0)) AS JustAskGuestSum, Sum(Nz([JustAskProgramCount1],0)+Nz([JustAskProgramCount2],0)+Nz([JustAskProgramCount3],0)) AS HouseGuidesProgramCount, Sum(Nz([HouseGuidesGuestSum1],0)+Nz([HouseGuidesGuestSum2],0)+Nz([HouseGuidesGuestSum3],0)) AS HouseGuidesGuestSum, Format(DateAdd("M", -6, [DateWorked]),"yyyy") AS myyear
FROM qryDocentCommitteeActivityStep1
GROUP BY Format(DateAdd("M", -6, [DateWorked]),"yyyy"), Format([DateWorked],"mmm")
ORDER BY [DateWorked];
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Suggested Courses

800 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