Solved

Order By Date

Posted on 2011-09-27
9
218 Views
Last Modified: 2012-05-12
Hi,

I've been asked a strange request to see if I can use the Order By Date in a query but display the list closet to today first - is this possible?

      SELECT id, company, requestor
      From tbl_oders
      Order By StartDate

Thanks

0
Comment
Question by:sanjshah12
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 125 total points
ID: 36713062
Depends on what you mean by "closest to today".  For example, this orders the list by DATEDIFF:

SELECT id, company, requestor
From tbl_oders
Order By DATEDIFF(day, StartDate, GETDATE())

Open in new window


An example of what you had in mind would be helpful.
0
 
LVL 21

Expert Comment

by:JestersGrind
ID: 36713113
I think it's even simpler.  Just order in descending order.

SELECT id, company, requestor
From tbl_oders
Order By StartDate DESC

Greg

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36713135
Greg,

Maybe :)

Depends on just what "closest to today" means, and whether all of the dates are in the future, all in the past, or a mix.  For example, theoretically it could be this:

SELECT id, company, requestor
From tbl_oders
Order By ABS(DATEDIFF(day, StartDate, GETDATE()))

Open in new window


That orders everything by "distance from today", without caring about the "direction" of the difference...

Patrick
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:sanjshah12
ID: 36713183
matthewspatrick,

yes I think yours solution is what I am looking for, what is the difference between the ABS and DateDiff query?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36713212
sanjshah12,

First things first.  

1) Please define exactly how you want the sorting to be done.  What are the rules?

2) Is StartDate always in the future, always in the past, or a mix?

3) Please provide a few sample rows of data, and indicate how those sample data should be sorted

Patrick
0
 

Author Comment

by:sanjshah12
ID: 36713294
Patirck,

1. the listing (sorting) should be form displayed from how many days it is away from todays date
2. the StartDate contains a mixture of dates
3. below is some sample data

Hop this makes sense.
1	user1	31/05/2011 00:00
2	user2	31/05/2011 00:00
3	user3	14/06/2011 00:00
4	user4	14/06/2011 00:00
5	user5	20/07/2011 00:00
6	user6	13/07/2011 00:00
7	user7	14/07/2011 00:00
8	user8	13/07/2011 00:00
9	user9	19/07/2011 00:00
10	user10	04/09/2011 00:00
11	user11	04/09/2011 00:00
12	user12	04/09/2011 00:00
13	user13	04/09/2011 00:00
14	user14	12/09/2011 00:00
15	user15	13/09/2011 00:00
16	user16	13/09/2011 00:00
17	user17	13/09/2011 00:00
18	user18	13/09/2011 00:00
19	user19	13/09/2011 00:00
20	user20	11/09/2011 00:00
21	user21	27/09/2011 00:00
22	user22	27/09/2011 00:00
23	user23	30/09/2011 00:00
24	user24	29/09/2011 00:00
25	user25	29/09/2011 00:00

Open in new window

0
 
LVL 3

Expert Comment

by:Srm74
ID: 36714629


As matthewspatrick,wrote,

SELECT id, company, requestor
From tbl_oders
Order By DATEDIFF(day, StartDate, GETDATE())

Open in new window


I believe that what you are looking for..

The ABS stands for Absolute, which means it removes the sign from the calculation..
I.e. if you had an order tomorrow, it would give the value -1 and so on..
Applying ABS turns this -1 into 1, giving tomorrow the same position as yesterday.

But i dont believe you have any orders for tomorrow, so you dont need this.

Hope it helps





0
 

Author Comment

by:sanjshah12
ID: 36716027
Yes SRM Patrick's answer is correct.
0
 
LVL 21

Expert Comment

by:JestersGrind
ID: 36716277
Patrick,

Good point about the direction.  I was assuming only past dates, which is probably a bad assumption.  Nice solution!

Greg

0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql 2014,  lock limit 5 37
SQL Query assistance 16 36
table joins in qry 17 61
Accessing variables in MySQL query 4 28
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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