Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 6th, 2007, 07:36 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 114
Thanks: 0
Thanked 0 Times in 0 Posts
Default ORDER BY Date Columns....

In a table, I have a column which is date and time (06/03/2007 11:35)

I need ORDER BY datecolumn, title so that if there are two entries on the same date (at different times) they will be ordered by the titles.

Considered:

ORDER BY LEFT(datecolumn, 10), title

But thought that there must be a 'more correct' way to do this.

Regards,

Sean Anderson
__________________
Regards,

Sean Anderson
 
Old March 6th, 2007, 08:43 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

I am assuming that this is a query.

In the Query Designer, put the DateColumn to the left of the Title column, then order the datecolumn, probably descending, and then order the Title column Ascending. Access will order the left-most column first, then the next one to the right, and so on. So you can extend this for the whole query.

Did that help?


mmcdonal
 
Old March 6th, 2007, 08:44 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

BTW, I see you have this question for a table. I don't think you can do this in a table, but you shouldn't be displaying data in a table anyway. Create a datasheet form, and base it on the query. Then show the user the datasheet, not the table. (design issues).

HTH

mmcdonal
 
Old March 6th, 2007, 08:50 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 114
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't think that works.

For example, the two columns contain the following, along with the expected order:

04/03/2007 08:00 - A (1st)
05/03/2007 10:30 - C (4rd)
05/03/2007 10:35 - A (2st)
05/03/2007 10:40 - B (3nd)
06/03/2007 09:45 - A (5th)

If you order by DATE, then TITLE, you get the order that is shown above, rather than the order defined in the parenthesis and you will see that the entrys for the 05/03/2007 are out of order.

Regards,

Sean Anderson
 
Old March 6th, 2007, 08:52 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 114
Thanks: 0
Thanked 0 Times in 0 Posts
Default

{QUOTE: BTW, I see you have this question for a table. I don't think you can do this in a table, but you shouldn't be displaying data in a table anyway. Create a datasheet form, and base it on the query. Then show the user the datasheet, not the table. (design issues).]

Sorry, poor terminology on my part. The end result is that the SQL Query will be used in an ASP page.

Regards,

Sean Anderson
 
Old March 12th, 2007, 01:59 AM
Registered User
 
Join Date: Feb 2007
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,
Use below query it will solve your requirement. Thanks to You, Because of your question I can share my experience with you.

NAME GRADE
-------------------------------
DEEPAK B (2nd)
KASHYAP A (1st)
KASHYAP B (2nd)
KASHYAP A (3rd)
SUSHIM C (1st)

SELECT tbM.Name, tbM.Grade, tbM.List
FROM tbM
ORDER BY tbM.Name, MID(tbM.Grade,4,1);

Regards,
KNSHARMA
 
Old March 12th, 2007, 04:22 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 114
Thanks: 0
Thanked 0 Times in 0 Posts
Default

But you have used a NAME for the first column, rather than a DATE.

I see what you have done, but there were only two columns in my example; the DATE, a TEXT column. The part where I have (1st) at the end, that was just to signify the order in which the results are expected to appear.

So for my table:

04/03/2007 08:00 - A
05/03/2007 10:30 - C
05/03/2007 10:35 - A
05/03/2007 10:40 - B
06/03/2007 09:45 - A

I am looking for this order:

04/03/2007 08:00 - A
05/03/2007 10:35 - A
05/03/2007 10:40 - B
05/03/2007 10:30 - C
06/03/2007 09:45 - A

Hope this clarifies the problem.

Regards,

Sean Anderson
 
Old March 15th, 2007, 03:56 PM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

The problem I'm seeing is that you want to sort only on the date portion of the date field when the date portion contains both date and time. If you look, the date field is sorted properly considering both date and time sort first in the order it's being sorted in.

To accommodate for this your query would have to take this into account.

This is the SQL I would suggest using:
---------------------------------------------------------------------------------------------------------
Select * From My_Table Order By Format([Date_Time], "mm/dd/yyyy"), [Letter_Grade];
---------------------------------------------------------------------------------------------------------

If for whatever reason this doesn't work for you as it did for me (I got the correct sort order you're looking for) then I'd suggest creating a field that holds only the date portion and sort on that field instead.

Hope this helps and explains what is happening for you.

P.S. Note the reason I'd use Format() if it works instead of left() is that format will extract the date the same regardless to how it's displayed in the actual table and doesn't require a length that may vary.
 
Old March 16th, 2007, 05:35 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 114
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Perfect idea.

Didn't realise that there was a "format" function available.

Thank you very much.

Regards,

Sean Anderson





Similar Threads
Thread Thread Starter Forum Replies Last Post
Order by based on two columns, each in two tables? LockesRabb PHP Databases 2 December 17th, 2006 12:19 PM
Order by based on two columns, each in two tables? LockesRabb SQL Language 4 December 14th, 2006 02:42 AM
problem displaying records in order of time ,date method PHP Databases 3 July 25th, 2006 08:16 PM
1. Subform = String, Chronological Date order nbuckwheat Access 2 December 21st, 2005 06:50 PM
* where date >= '"& dateVar &"' order by date wrofox Classic ASP Databases 5 February 24th, 2005 10:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.