简体   繁体   中英

How can I handle a Julian Date rollover in SQL?

I have a list of julian dates that I need to keep in order ex. 362, 363, 364, 365, 001, 002, 003. My query starts with getting the last julian date processed and each date after that. Right now it will max my lowest date out at 365 and I can't get the records that follow it. The same set of data also has a date field with the year attached but it doesn't seem to be helpful since those records won't be gathered until the rollover is corrected. Here is my simplified query:

select JulianDate, RecordDate 
from table 
where JulianField > @LowestJulianDate
and RecordDate between GetDate() and DateAdd(day, 6, GetDate())

Sample date:

JulianDate RecordDate
362 2020-12-28
363 2020-12-29
364 2020-12-30
365 2020-12-31
001 2021-01-01
002 2021-01-02
003 2021-01-03

Desired output:

JulianDate
362
363
364
365
001
002
003

So if you'll imagine we start on day 362, our @LowestJulianDate is 362, and our record date range is today and the next 6 days, completing that list of julian dates.

How can I get the dates to go in order and resolve in a rollover?

well why not sorting by year column and Julian date column?

select JulianDate, RecordDate 
from table 
order by yearcolumn,JulianDate

You cannot by just using the "JulianDate" which is actually the DayOfYear value. You would need to also store the year that it refers to either separately or as part of the "JulianDate" value. For example, instead of "362" you need "2021362".

What we are doing in the case of not having a year and wanting to sort a list on the year rollover for a 7 day rolling window is looking at the left 1 of the Julian day. If it's less than 3 roll it's rolled over. We sort into 2 baskets (old year and new year), order them, then recombine them with the new year's data being the "greatest" in the list.

We look at the left 1 because in our application, the last day of data we get may be 357 and the rollover may be 003 for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM