Monthly Sequence Sorting Order in Crosstab Query (1 Viewer)

ahmad_rmh

Member
Local time
Today, 09:44
Joined
Jun 26, 2022
Messages
243
How to make the monthly sequence sorting order in crosstab query.

I have attached the screen shots of the query.

The criteria which have been used for the query is as under;

TRANSFORM Sum(Stock_In_Out) AS SumOfStock_In_Out
SELECT ItemsPK, ItemsListName
FROM qryTransactionsExtended
WHERE (((EntityPK)>4))
GROUP BY ItemsPK, ItemsListName
ORDER BY ItemsPK, Format([TranDate],"mmm - yyyy") DESC
PIVOT Format([TranDate],"mmm - yyyy");
 

Attachments

  • Capture.JPG
    Capture.JPG
    37.2 KB · Views: 65
  • Capture1.JPG
    Capture1.JPG
    19.9 KB · Views: 67
Last edited:

June7

AWF VIP
Local time
Yesterday, 22:44
Joined
Mar 9, 2014
Messages
5,471
Format([TranDate], "yyyy-mm")

or

specify column headers as described in article.

Click the "dynamic monthly crosstab reports" link in that article for more information on solution to your situation.
 

ahmad_rmh

Member
Local time
Today, 09:44
Joined
Jun 26, 2022
Messages
243
Format([TranDate], "yyyy-mm")

or

specify column headers as described in article.

Click the "dynamic monthly crosstab reports" link in that article for more information on solution to your situation.

Thanks It's working,

What's the logic used in programming "yyyy-mmm-dd"
 

June7

AWF VIP
Local time
Yesterday, 22:44
Joined
Mar 9, 2014
Messages
5,471
If dataset is multi-year and query retrieves data that crosses years, output will be in chronological order. However, cannot use mmm because that returns month short name and order is alphabetical.
 

ahmad_rmh

Member
Local time
Today, 09:44
Joined
Jun 26, 2022
Messages
243
If dataset is multi-year and query retrieves data that crosses years, output will be in chronological order. However, cannot use mmm because that returns month short name and order is alphabetical.

But how could I get year name at the end?
 

ahmad_rmh

Member
Local time
Today, 09:44
Joined
Jun 26, 2022
Messages
243

June7

AWF VIP
Local time
Yesterday, 22:44
Joined
Mar 9, 2014
Messages
5,471
I don't know any other way to force this in query. You can build a report and put what you want in labels. Did you review the linked article in Allen Brown's tutorial?

You could write a bunch of VBA code and save data to a temp table.
 

ebs17

Well-known member
Local time
Today, 08:44
Joined
Feb 7, 2020
Messages
1,946
I want month name before year
If you want:
Code:
...
PIVOT Format([TranDate],"mmm - yyyy") IN ("Jan - 2022", "Feb - 2022", ...)
Use fixed columns. In your fixation you can make a "sorting" independently.

You just have to be aware that in a crosstab query column names are generated dynamically from existing data. Dynamic and fixation have a conflict potential against each other.
 

ahmad_rmh

Member
Local time
Today, 09:44
Joined
Jun 26, 2022
Messages
243
If you want:
Code:
...
PIVOT Format([TranDate],"mmm - yyyy") IN ("Jan - 2022", "Feb - 2022", ...)
Use fixed columns. In your fixation you can make a "sorting" independently.

You just have to be aware that in a crosstab query column names are generated dynamically from existing data. Dynamic and fixation have a conflict potential against each other.

I don't want to fix column names as I have already mentioned in post #9
 

ebs17

Well-known member
Local time
Today, 08:44
Joined
Feb 7, 2020
Messages
1,946
No matter what you want: without fixed columns, they will be sorted alphabetically, and this will start with the first character, then the second, and so on.

So get your month names to line up alphabetically to match your desired order.
 

ebs17

Well-known member
Local time
Today, 08:44
Joined
Feb 7, 2020
Messages
1,946
As I said: You just have to be aware that in a crosstab query column names are generated dynamically from existing data.

You can create a sequence number on the month in the query, which would also be dynamic, and then use it.
Code:
PIVOT Format(SeqNumb, "00") & Format([TranDate]," - mmm - yyyy")
But this also detracts from the beauty of the view.
 
Last edited:

ahmad_rmh

Member
Local time
Today, 09:44
Joined
Jun 26, 2022
Messages
243
As I said: You just have to be aware that in a crosstab query column names are generated dynamically from existing data.

You can create a sequence number on the month in the query, which would also be dynamic, and then use it.
Code:
PIVOT Format(SeqNumb, "00") & Format([TranDate]," - mmm - yyyy")
But this also detracts from the beauty of the view.

I have attached a sample db file, I have tried but not getting the results what i am expecting
 

Attachments

  • testdb.accdb
    736 KB · Views: 75

ebs17

Well-known member
Local time
Today, 08:44
Joined
Feb 7, 2020
Messages
1,946
what i am expecting
You have to adjust your expectations to the given possibilities. As an example:
Code:
...
PIVOT
   Format([Date], "yyyy_mm - mmmm")
The alternative is a larger VBA project that must also accommodate and handle dynamics and transform query results into desired views.
You have to pay for such additional effort as well as probably compromise again elsewhere.
 

ahmad_rmh

Member
Local time
Today, 09:44
Joined
Jun 26, 2022
Messages
243
You have to adjust your expectations to the given possibilities. As an example:
Code:
...
PIVOT
   Format([Date], "yyyy_mm - mmmm")
The alternative is a larger VBA project that must also accommodate and handle dynamics and transform query results into desired views.
You have to pay for such additional effort as well as probably compromise again elsewhere.

How the programmers are implementing months as like mmmm - yyyy
 

ebs17

Well-known member
Local time
Today, 08:44
Joined
Feb 7, 2020
Messages
1,946
Use a search on the internet with a search engine of your own choice and for example the following keywords:
crosstab query report
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 02:44
Joined
Feb 19, 2002
Messages
43,275
How the programmers are implementing months as like mmmm - yyyy
They are creating the SQL using VBA every time they run the query. That lets them keep changing the column headings programmatically. They are not hard coded.
 

Users who are viewing this thread

Top Bottom