Solved

How sort query results based on a formatted date field

Posted on 2014-01-24
8
428 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
  • 4
  • 3
8 Comments
 
LVL 15

Expert Comment

by:OMC2000
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:SteveL13
Comment Utility
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
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!

 
LVL 15

Expert Comment

by:OMC2000
Comment Utility
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
Comment Utility
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 40

Expert Comment

by:Sharath
Comment Utility
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 500 total points
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
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…
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 …

772 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

12 Experts available now in Live!

Get 1:1 Help Now