Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How sort query results based on a formatted date field

Posted on 2014-01-24
8
Medium Priority
?
457 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
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…

597 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