Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query by Month

Status
Not open for further replies.

mring42

Technical User
Jul 8, 2004
12
US
When I try to group by month, sometimes rather than having the output as:

January 2003
February 2003
April 2003
May 2003
April 2004
May 2004

The output will be:

April 2003
April 2004
February 2003
January 2003
May 2003
May 2004

This only happens when I have many, many columns, but it's very difficult to see trends like that...
Thanks,
Mike
 
You are grouping by an alpha field so it is sorting by the Alpha representation of the Month Year. Create a column in the reverse, Year Month with numeric only and group by that column. The column that displays the month year as you wish should not be grouped but be identified as an expression.

Code:
Select . . . 


Order by (Year([datefield]) & Month([datefield]))
Group by (Year([datefield]) & Month([datefield]))

Above is an example. Just supply your current code and I will update it appropriately if you need further help.

[COLOR=006633]Bob Scriver[/color]
MIState1.gif
[COLOR=white 006633]MSU Spartan[/color]
 
Add a column to the query where you format the date as "yyyymm" and sort on that instead. You can uncheck the "Display" box if you don't want this column in your output.
 
Thanks a lot for your help

Here's the Group By statement:

GROUP BY Format$([Birds-All_Crosstab].[Date],'mmmm yyyy'), Year([Birds-All_Crosstab].[Date])*12+DatePart('m',[Birds-All_Crosstab].[Date])-1;
 
Is this your current Group By or a new one you are have tried to implement after reading the postings?

[COLOR=006633]Bob Scriver[/color]
MIState1.gif
[COLOR=white 006633]MSU Spartan[/color]
 
This is what I was using before reading the postings.
 
You can leave your Select portion of the query intact so that the display of the data looks the same. Just change the Group By and Order By to the following and the records will group and display in the correct combinations and sorting order.


Code:
GROUP BY Year([Birds-All_Crosstab].[Date]) & Format([Birds-All_Crosstab].[Date],"mm") 
ORDER BY Year([Birds-All_Crosstab].[Date]) & Format([Birds-All_Crosstab].[Date],"mm");

Post back if you have any questions.

[COLOR=006633]Bob Scriver[/color]
MIState1.gif
[COLOR=white 006633]MSU Spartan[/color]
 
Thanks for your help. I had to tweak the Select statement slightly because the expression that I was using to group the Date column had to be changed, but I figured it out, and it's all working now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top