Solved

How sort query results based on a formatted date field

Posted on 2014-01-24
8
444 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
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: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 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

730 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