How can I handle a Julian Date rollover in SQL? - 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?

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".

well why not sorting by year column and Julian date column ?
select JulianDate, RecordDate
from table
order by yearcolumn,JulianDate

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.

Related

Calculate Week Numbers based on the initial given date to end date

I have below scenario that Business want to calculate Week Number based on Given Start Date to End Date.
For Ex: Start Date = 8/24/2020 End Date = 12/31/2020 ( These Start date & end date are not constant they may change from year to year )
Expected Output below:
[Date 1 Date 2 Week Number
8/24/2020 8/30/2020 week1
8/31/2020 9/6/2020 week2
9/7/2020 9/14/2020 week3
9/15/2020 9/21/2020 week4
9/22/2020 9/28/2020 week5
9/29/2020 10/5/2020 week6
10/6/2020 10/12/2020 week7
10/13/2020 10/19/2020 week8
10/20/2020 10/26/2020 week9
10/27/2020 11/02/2020 week10
11/03/2020 11/09/2020 week11
11/10/2020 11/16/2020 week12
11/17/2020 11/23/2020 week13
11/24/2020 11/30/2020 week14
I need Oracle Query to calculate Week Number(s) like above .. Based on Start date for 7 days then week number will be calcuated.. But remember that crossing months some month have 30 days and some month 31 days etc.. How to calculate ? Appreciate your help!!
Seems your looking for custom week definition rather that built-ins. But not overly difficult. The first thing is to convert from strings to dates (if columns actually coming off table this conversion is not required), and from there let Oracle do all the calculations as you can apply arithmetic operations to dates, except adding 2 dates. Oracle will automatically handle differing number of days per month correctly.
Two methods for this request:
Use a recursive CTE (with)
with dates(start_date,end_date) as
( select date '2020-08-24' start_date
, date '2020-12-31' end_date
from dual
)
, weeks (wk, wk_start, wk_end, e_date) as
( select 1, start_date, start_date+6 ld, end_date from dates
union all
select wk+1, wk_end+1, wk_end+7, e_date
from weeks
where wk_end<e_date
)
select wk, wk_start, wk_end from weeks;
Use Oracle connect by
with dates(start_date,end_date) as
( select date '2020-08-24' start_date
, date '2020-12-31' end_date
from dual
)
select level wk
, start_date+7*(level-1) wk_start
, start_date+6+7*(level-1)
from dates
connect by level <= ceil( (end_date-start_date)/7.0);
Depend on how strict you need to be with the end date specified you may need to adjust the last row returned. Both queries do not make adjust for that. They just ensure no week begins after that date. But the last week contains the full 7 days, which may end after the specified end date.
If your date datatype is varchar then first convert it to date and then convert it back to varchar.
convert date to to_char(to_date('8/24/2020','MM/DD/YYYY'),'WW')
If you to keep week datatype as a number then you can do something like this
to_number(to_char(to_date('8/24/2020','MM/DD/YYYY'),'WW'))
Few options according to your need.
WW Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
W Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.
IW Week of year (1-52 or 1-53) based on the ISO standard.

How to retrieve data within a 6 day time period

I want to retrieve the data between a 6 day time period.
The output I want is:
Date
--------
2019-05-01
2019-05-04
2019-06-01
2019-06-06
2019-07-01
This is my query so far:
select date from data d
where CAST(d.createdate as Date) between CAST('2019-05-01' as Date)
AND DATEADD(CAST(dd,6,'2016-07-01') as Date)
Why is this not retrieving the results I want?
You have several problems with your query.
The first is with your DATEADD statement which is all mixed up. You are not nesting the casted date into the statement properly. This is the corrected version:
DATEADD(dd, 6, CAST('2016-07-01' as Date))
The second is that your select projection refers to the column date which does not exist. Instead, you probably want your createdate column.
The third is that your between clause is back to front. You are saying between 2019-05-01 and 2016-07-01 but the smaller date must come first.
In fact, your given example is incorrect. In your question, you say "want to retrieve the data between two dates only for 6 days." So, why would you start with a date in 2016 and then jump to a date in 2019 and add 6 days to the date in 2019? If you want to use the DATEADD approach, you need to use the same date in both positions.
So here is your corrected query:
select d.createdate from data d
where CAST(d.createdate as Date) between CAST('2019-05-01' as Date)
AND DATEADD(dd, 6, CAST('2019-05-01' as Date))

QV - Count days in a year in script

In the script I need a counter to give the same day (in different years) a number, and when a new year begin it will start from 1 again. So when I reload my script I want a table that look like this:
Date Number
01/01/2015 1
...... ...
10/30/2015 303
10/31/2015 304
11/01/2015 305
.... ...
12/31/2015 365
01/01/2016 1
01/02/2016 2
.... ....
How can I do this?
DayNumberOfYear(date[,firstmonth])
Returns the day number of the year according to a timestamp with the first millisecond of the first day of the year containing date. The function always uses years based on 366 days.
By specifying a firstmonth between 1 and 12 (1 if omitted), the beginning of the year may be moved forward to the first day of any month. If you e.g. want to work with a fiscal year starting March 1, specify firstmonth = 3.
You could try subtracting the Date from the beginning of the year of the Date
Date-makedate(year(Date),1,1)+1 as Number
You'll need a bit more maths if leap years are going to matter in you analysis.

Earliest and Lastdate for each year in sql

I have a column with 3 columns. I have multiple records for a year. As you see some of my records as follows
ID stardate enddate
1 1/1/2010 5/3/2010
2 2/4/2010 NULL -**EDIT**
3 1/2/2011 5/6/2011
4 3/4/2011 NULL -**EDIT**
I want to get a result for the earliest date in that year and the last date in that year. So output could be like
**EDITED:** 1/1/2010 12/31/2010 - For Year 2010
**EDITED:** 1/2/2011 12/31/2011 - For Year 2011
How can i get that in a query?If you need more info,please ask. Thanks
EDIT: If for the year if one of the columns read NULL then I have to consider the last day of the year as the enddate. i.e.12/31/YYYY. And I need to do that for each year again.
Assuming you use DATE (or related) columns in a MySQL table, something like this should serve your request:
SELECT MIN(startdate),
MAX(enddate),
YEAR(startdate)
FROM my_table
GROUP BY YEAR(startdate);
This groups all entries by year (of the startdate) and show you the minimum and maximum entries for each year as you want. See also the documentation for the DATE function in MySQL.
There are similar date functions and possibilities if you are using an other database system. Usually you can easily find them by googling the database system and something like "date functions".
select MIN(stardate),max(enddate)
from [Tablename]
where YEAR(enddate)=2013

firebird sql month time cumulated

I am trying to get the following out my firebird db:
I have records with a Timestamp attribute and an attribute with a currency value. Now I want query the currency value cumulated by month and now comes the tricky thing for me: I want each month starting from the first day at 16:30:00 p.m til the next first day in the next month at 16:29:59. I am not sure how to add this in the where condition or somewhere else? Thx for helping me!
eg.
Timestamp Value
...
2012-07-31 17:05:03 23,--
2012-08-01 12:23:15 15,--
cut:
2012-08-01 16:35:56 25,--
2012-08-02 12:23:15 5,--
2012-08-25 18:03:34 6,--
2012-08-31 08:03:55 9,--
2012-09-01 12:23:15 7,--
cut:
2012-09-01 16:47:43 3,--
2012-09-02 19:13:10 8,--
2012-09-10 19:13:10 18,--
2012-09-30 19:13:10 3,--
2012-10-02 19:13:10 8,--
....
July: 23,-- + 15,-- = 38,-
August: 52,--
September: 32,--
Oktober: 8,-- and so on...
You need to shift the date back (or forward) so that the start time of this month falls on 00:00 of the first of the month, and then simply group by the year and month part of the timestamp:
With a simple table I get the expected results using:
WITH shifteddate AS (
SELECT DATEADD(MINUTE, -30, DATEADD(HOUR, -16, infodate)) shifteddate, infovalue
FROM SUMEXAMPLE
)
SELECT EXTRACT(YEAR FROM shifteddate), EXTRACT(MONTH FROM shifteddate), SUM(infovalue)
FROM shifteddate
GROUP BY 1, 2
You could also use subquery instead of a CTE, but I just find this easier to read.