Solved

Order by date question

Posted on 2004-08-10
12
178 Views
Last Modified: 2013-12-24
I was wondering if there is a way to sort my query by date.  But instead of having it sort the normal way (either ASC or DESC), is there a way to have it sort by the item that is closest to today's date and then move on.

So for instance.  Say I have 10 items in my database ranging from Jan 04 to Dec 04.  Is it possible to list the item closes to August 8th first and the forward and possibly moving the earlier items to the bottom of the page.

If anything.  How can I display the item that is closest to todays date and future items.

Thank you,
Jeff
0
Comment
Question by:jeffmace
  • 4
  • 3
  • 3
12 Comments
 
LVL 4

Expert Comment

by:eclipse2k
ID: 11763228
Hello Jeff!!

This is something, your SQL Server should manage. If you are using Microsoft SQL Server, you can do it that way:

-----------------
select      *
from      yourtable
order      by Abs(DateDiff(s, YOUR_DATE, DATE_COLUMN)) ASC
-----------------

for example

-----------------
select      *
from      yourtable
order      by Abs(DateDiff(s, '2004-25-05', [Timestamp])) ASC
-----------------

There is still a little problem, though: This selection is quite accurate, because the difference is measured in seconds (see the S in DateDiff function). But when you exceed the timespan of 68 Years, it will cause an error, an you will need to use MI for minutes :)


Eclipse2k

0
 
LVL 4

Expert Comment

by:eclipse2k
ID: 11763273
Its me again,
here is a real example i tested:

-----------------
select      TOP 25 [Timestamp]
from      logging
order      by Abs(DateDiff(s, '2004-27-07 12:30:00', [Timestamp])) ASC
-----------------

Results:

-----------------
2004-07-27 12:34:52.760
2004-07-27 12:34:53.167
2004-07-27 12:24:28.780
2004-07-27 12:24:28.860
2004-07-27 12:37:50.183
2004-07-27 12:37:50.230
2004-07-27 12:37:50.230
2004-07-27 12:45:37.847
2004-07-27 12:45:37.893
2004-07-27 12:45:37.910
2004-07-27 12:13:35.337
2004-07-27 12:13:35.400
2004-07-27 12:13:32.260
2004-07-27 12:13:32.320
2004-07-27 12:13:31.040
2004-07-27 12:13:30.993
2004-07-27 12:13:29.010
2004-07-27 12:13:28.947
2004-07-27 13:13:08.030
2004-07-27 13:13:08.093
2004-07-27 13:14:31.743
2004-07-27 13:14:31.773
2004-07-27 13:15:27.257
2004-07-27 13:15:27.287
2004-07-27 13:15:27.303
-----------------

Eclipse2k
0
 

Author Comment

by:jeffmace
ID: 11763356
Here is my query:  I am not sure how this works with this [TimeStamp] function.  I am using SQL 2000, but I am not a pro at SQL.

<CFQUERY name="getMeetings" datasource="#ds#">
SELECT     CMEProgramSym.*, CMEPrograms.CMEProgramName, CMEPrograms.CMEProgramSymposiumName,
                      Users.*
FROM         CMEProgramSym INNER JOIN
                      CMEPrograms ON CMEProgramSym.CMEProgramID = CMEPrograms.CMEProgramID INNER JOIN
                      Users ON CMEPrograms.CMEProgramSalesRep = Users.UserID
WHERE CMEProgramSym.CMEProgramID = '#URL.ID#'
ORDER by Abs(DateDiff(s, CMEProgramSym.CMEProgramSymDate, [TimeStamp])) ASC
</CFQUERY>


The CMEProgramSymDate field is the date field that will help sort the order.
0
 
LVL 35

Accepted Solution

by:
mrichmon earned 250 total points
ID: 11763799
put NOW() in where you have [Timestamp]
0
 

Author Comment

by:jeffmace
ID: 11764013
Well I got the query to work, but the order is very messed up now even though its correct by the datediff function.

08/11/04
08/3/04
08/18/04
07/21/04
07/14/04
09/09/04
07/08/04
09/22/04

I was hoping for it to start here at the 8/11 and move on forward. so at least I can have the most current meeting at the top.
0
Free camera licenses with purchase of My Cloud NAS

Milestone Arcus software is compatible with thousands of industry-leading cameras for added flexibility. Upon installation on your My Cloud NAS, you will receive two (2) camera licenses already enabled in the software. And for a limited time, get additional camera licenses FREE.

 
LVL 4

Assisted Solution

by:eclipse2k
eclipse2k earned 250 total points
ID: 11764328
@mrichmon

i guess that diffing with Now() would not return the expected result, because the task is to check from Any given point of time. Your solution would be finally the same like ordering by CMEProgramSym.CMEProgramSymDate (in the given case)

@jeff

i really wonder about the results you get...
Please check that...

 - CMEProgramSymDate is a DateTime SQL Variable Type..?
 - Where [TimeStamp] stands, you in fact need the date to be checked, like... '2004-08-08 12:00:00' or anything like that (i dont know your locale Date Format since i use the german)

Possible example, not tested:

-------------------
<!--- CreateDateTime Arguments: Year, Month, Day, Hour, Minute, Second --->
<cfset checkDate = CreateDateTime(2004,8,8,12,0,0)>

<CFQUERY name="getMeetings" datasource="#ds#">
SELECT     CMEProgramSym.*, CMEPrograms.CMEProgramName, CMEPrograms.CMEProgramSymposiumName,
                      Users.*
FROM         CMEProgramSym INNER JOIN
                      CMEPrograms ON CMEProgramSym.CMEProgramID = CMEPrograms.CMEProgramID INNER JOIN
                      Users ON CMEPrograms.CMEProgramSalesRep = Users.UserID
WHERE CMEProgramSym.CMEProgramID = '#URL.ID#'
ORDER by Abs(DateDiff(s, CMEProgramSym.CMEProgramSymDate, <cfqueryparam cfsqltype="cf_sql_date" value="#checkDate#">)) ASC
</CFQUERY>
-------------------

eclipse2k
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 11765249
Not really.  jeffmace mentioned "How can I display the item that is closest to todays date" so in that case Now() would work.
0
 

Author Comment

by:jeffmace
ID: 11765347
CMEProgramSymDate is smalldatetime in SQL and it looks like this in the database '5/24/2003'  

Maybe that will help clear up the issue, I am not sure if there is a different way to code for that setup.

Jeff
0
 
LVL 4

Expert Comment

by:eclipse2k
ID: 11778613
@Jeff

It looks like you dont have a Date Format without time?
You could try to use "mi" instead of "s" for the DateDiff Function.

Also you could check out:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_da-db_5vxi.asp

Quote from site: "...Because smalldatetime is accurate only to the minute, when a smalldatetime value is used, seconds and milliseconds are always 0."


@mriichmon

Sorry, i got it wrong because i read "list the item closes to August 8th..." and i really didnt notice that this was the post date :) I'm sorry, you were right
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 11778702
it's ok :o)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now