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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ORDER BY month problem 1

Status
Not open for further replies.

jaydeebe

IS-IT--Management
Nov 14, 2003
169
GB
I have a query as follows -

SELECT Format([StartDate],"mmm") AS Month, Count(*) AS Bookings
FROM tblbookings
WHERE (((tblbookings.StartDate) Between #12/31/2004# And #12/31/2005#))
GROUP BY Format([StartDate],"mmm");

I want to order the results by month but when I try the list is in alphabetical order instead of month order. Can anyone help me?
 
SELECT Format([StartDate],"mmm") AS Month, Count(*) AS Bookings
FROM tblbookings
WHERE (((tblbookings.StartDate) Between #12/31/2004# And #12/31/2005#))
GROUP BY Format([StartDate],"mmm")
ORDER BY [StartDate];

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Try:
Code:
SELECT Format([StartDate],"mmm") AS Month, Count(*) AS Bookings
FROM tblbookings
WHERE (((tblbookings.StartDate) Between #12/31/2004# And #12/31/2005#))
GROUP BY Format([StartDate],"mmm")
ORDER BY Month([StartDate]);

Duane MS Access MVP
[green]Ask a great question, get a great answer.[/green] [red]Ask a vague question, get a vague answer.[/red]
[green]Find out how to get great answers faq219-2884.[/green]
 
PHV - I tried that and i get the message - You tried to execute a query that doesn't include the specified expression '[StartDate]' as part of an aggregate function.

Duane - I tried that and i get the message - You tried to execute a query that doesn't include the specified expression 'Month([StartDate])' as part of an aggregate function.

 
SELECT Format([StartDate],"mmm") AS Month, Count(*) AS Bookings, Month([StartDate]) as MthNum
FROM tblbookings
WHERE (((tblbookings.StartDate) Between #12/31/2004# And #12/31/2005#))
GROUP BY Format([StartDate],"mmm")
ORDER BY Month([StartDate]);


Duane MS Access MVP
[green]Ask a great question, get a great answer.[/green] [red]Ask a vague question, get a vague answer.[/red]
[green]Find out how to get great answers faq219-2884.[/green]
 
BTW r937 can you explain how that works. I don't get how grouping by makes the difference.

SELECT Format([StartDate],"mmm") AS Month, Count(*) AS Bookings
FROM tblbookings
WHERE (((tblbookings.StartDate) Between #12/31/2004# And #12/31/2005#))
GROUP BY Format([StartDate],"mmm"), Month([StartDate])
ORDER BY Month([StartDate]);
 
sure, no prob

Format([StartDate],"mmm") is a 3-char string, while Month([StartDate]) is a number

the optimizer, clever as it is, doesn't realize they mean the same thing, it just recognizes that they are not exactly the same thing, nor are they literal values (which do not have to be in the GROUP BY), so both of them have to be in the GROUP BY

thus SELECT A,3 FROM ... GROUP BY A is okay

but SELECT A,B FROM ... GROUP BY A is not okay



rudy | r937.com | Ask the Expert | Premium SQL Articles
SQL for Database-Driven Web Sites (next course starts March 6 2005)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top