Solved

Order By Date

Posted on 2011-09-27
9
210 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

20 Experts available now in Live!

Get 1:1 Help Now