473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Order by date range

I want to get all the records from a table (no grouping) and order them first by a date range and then within that range order them by another column. For example, I have a table called Events:

ID | Date | Latitude
--------------------------------------------------------
1 | 12-01-2008 | 6
2 | 12-04-2008 | 4
3 | 12-05-2008 | 12
4 | 12-02-2008 | 4

So in this example I run the query on 12-5-2008 and would like the date range to be every two days, and the second 'order by' item to be the latitude. So the output would be:

ID | Date | Latitude
--------------------------------------------------------
2 | 12-04-2008 | 4
3 | 12-05-2008 | 12
4 | 12-02-2008 | 4
1 | 12-01-2008 | 6



ID 2 and 3 are within the last 2 days, so 2 comes before 3 (since I'm also ordering on Lat). The next date range is day 3 and 4, so ID 4 comes since it is the only item in that range, etc...

Thanks a ton for any help.

Dan
Oct 28 '08 #1
1 2303
ck9663
2,878 Expert 2GB
Use CASE on your ORDER BY.

Try this:

Expand|Select|Wrap|Line Numbers
  1. set nocount on
  2.  
  3. declare @rundate smalldatetime
  4. declare @yourtable table (id int, date smalldatetime, latitude int)
  5.  
  6.  
  7. insert into @yourtable values(1 , '12-01-2008' , 6)
  8. insert into @yourtable values(2 , '12-04-2008' , 4)
  9. insert into @yourtable values(3 , '12-05-2008' , 12)
  10. insert into @yourtable values(4 , '12-02-2008' , 4)
  11.  
  12. set @rundate = '12-05-2008'
  13.  
  14. select *
  15. from @yourtable
  16. order by 
  17. case
  18.     when datediff(dd,date, @rundate) <= 2 then
  19.  
  20.     else 1
  21. end asc, latitude
Happy coding!

-- CK
Oct 28 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
1
by: Supreme | last post by:
Hello! I am trying to structure the order of a series of forms that make up one record. Basically, I would like to detect the day of the week, and based on the day send the end user to a particular...
5
by: BlackFireNova | last post by:
I need to write a report in which one part shows a count of how many total records fall within the working days (Monday - Friday) inside of a (prompted) given date range, in a particular...
12
by: Steve Elliott | last post by:
I have a query set up to gather together data between two specified dates. Shown in the query column as: Between #24/09/2004# And #01/10/2004# Is it possible to enter several different date...
3
by: manning_news | last post by:
Using A2K. I've been asked to modify a report currently requiring only one date parameter to now accept a date range. The main report has 2 subreports and is not bound to a table or query. The...
18
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
19
by: ali3n8 | last post by:
Hello I have attempted to create a date range report from a query called qrycustomerinformation. The field that contains the value of my date is called Followup. When i run a report on this it is...
4
Sandboxer
by: Sandboxer | last post by:
I want to be able to program Access to provide for me, by individual day, what my contract obligations are to my customers. Will Access recognize all the individual days in between a date range...
2
by: grego9 | last post by:
I have a problem in Excel 2000. I have written some VBA code that transfers data from the current excel workbook to a file called "Deal Input2.xls". Everything transfers ok apart from the date in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.