Solved

Order by date question

Posted on 2004-08-10
12
183 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

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…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 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