Sorting Dates with query

psycoperl

Active Member
Joined
Oct 23, 2007
Messages
339
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
  3. Web
I have what seems to be an easy question I am trying to sort dates in my query however it does not sort chronologically. Can someone please provide guidance as to why this is not working as intended/needed?

Query:
Code:
SELECT DISTINCTROW Format$([qryAllData_ByDateSite].[ADate],'mmmm yyyy') AS ADate_Month, 
getfiscalyear([ADate]) AS AYear, getfiscalmonth([ADate]) AS aMonth, 
qryAllData_ByDateSite.Site, 
Sum(qryAllData_ByDateSite.SumOfdelivQty) AS delivQty, 
Sum(qryAllData_ByDateSite.SumOfreadQty) AS readQty, 
Sum(qryAllData_ByDateSite.SumOfpackQty) AS packQty, 
Sum(qryAllData_ByDateSite.SumOfpickQty) AS pickQty
FROM qryAllData_ByDateSite
GROUP BY Format$([qryAllData_ByDateSite].[ADate],'mmmm yyyy'), 
getfiscalyear([ADate]), 
getfiscalmonth([ADate]),
 qryAllData_ByDateSite.Site, 
Year([qryAllData_ByDateSite].[ADate])*12+DatePart('m',[qryAllData_ByDateSite].[ADate])-1
ORDER BY getfiscalyear([ADate]), getfiscalmonth([ADate]);


I am getting:
Code:
ADate_Month	AYear	aMonth	Site	delivQty	readQty	packQty	pickQty
April 2012	2012	10	02	38	20	20	20
April 2012	2012	10	12	572	572	572	617
May 2012	2012	11	16	354	354	354	314
May 2012	2012	11	20	561	562	562	526
June 2012	2012	12	02	44	44	44	44
June 2012	2012	12	12	771	771	771	701
February 2012	2012	8	12	249	13	13	13
March 2012	2012	9	16	134	134	134	128
March 2012	2012	9	20	207	207	207	207
July 2012		2013	1	02	34	34	34	34
July 2012		2013	1	12	816	810	810	950
April 2013	2013	10	16	184	184	184	157
April 2013	2013	10	20	125	125	125	125
May 2013	2013	11	02	19	27	27	27
May 2013	2013	11	12	1388	1388	1388	988
June 2013	2013	12	16	83	83	83	173
June 2013	2013	12	20	387	387	387	387
August 2012	2013	2	02	35	35	35	35
August 2012	2013	2	20	311	364	364	364
September 2012	2013	3	02	5	0	0	0
October 2012	2013	4	20	12	12	12	12
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
It appears that getfiscalyear([ADate]) and getfiscalmonth([ADate]) are functions that return text. So you are getting a textual sort order (where, for instance, 10 is less than 9 because it starts with 1).

You could try (might work, not sure):

Code:
ORDER BY CLng(Nz(getfiscalyear([ADate]), 0)), CLng(Nz(getfiscalmonth([ADate]),0))
 
Upvote 0
It appears that getfiscalyear([ADate]) and getfiscalmonth([ADate]) are functions that return text. So you are getting a textual sort order (where, for instance, 10 is less than 9 because it starts with 1).

You could try (might work, not sure):

Code:
ORDER BY CLng(Nz(getfiscalyear([ADate]), 0)), CLng(Nz(getfiscalmonth([ADate]),0))

Thank you! That works... Its now in chrono order! Great!
 
Upvote 0
how is April the 10th month ?


Rich (BB code):
Rich (BB code):
ADate_Month    AYear    aMonth    Site    delivQty    readQty    packQty    pickQty
April 2012    2012    10    02    38    20    20    20
 
Upvote 0

Forum statistics

Threads
1,221,687
Messages
6,161,287
Members
451,695
Latest member
Doug Mize 1024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top