Solved

Order by date question

Posted on 2004-08-10
12
180 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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
 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
json_decode return null? 8 86
Problem to get function 52 106
Domino Website - Redirection 12 74
Point a domain to a 4rd party web host without changing nameservers 2 27
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

11 Experts available now in Live!

Get 1:1 Help Now