Monthly Sequence Sorting Order in Crosstab Query

ahmad_rmh

Member
Local time
Today, 11:35
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: 103
  • Capture1.JPG
    Capture1.JPG
    19.9 KB · Views: 97
Last edited:
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.
 
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"
 
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.
 
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?
 
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.
 
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.
 
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
 
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.
 
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:
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

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.
 
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
 
Use a search on the internet with a search engine of your own choice and for example the following keywords:
crosstab query report
 
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

Back
Top Bottom