Days Unavailable - sql

I need a simple SQL to accomplish the below:
Problem:
When a petrol bunk runs out of fuel, the admin makes note of the DateTime (RunOutDate) when it ran out of fuel and notes also the DateTime (ResupplyDate) when the fuel supply was back on.
I need to create a report on how many days the bunk ran out of fuel.
eg.
1/1/1 10:10 to 1/1/1 10:50 should be counted as 1
1/1/1 10:10 to 2/1/1 07:20 should be counted as 2
1/1/1 23:55 to 2/1/1 00:10 should be counted as 2
I can not bank using hours using DateDiff as 24 hours could have spanned across 2 days.
TIA

DATEDIFF(d, RunOutDate, ResupplyDate) + 1
Remember that DATEDIFF always counts the number of BOUNDARIES that you cross. For days (first argument d), it counts the number of times the clock passed midnight. So to count the number of days covered you just add 1.

DATEDIFF using day, then add 1.
DATEDIFF uses the midnight to count days so you'll get 0, 1, 1 for each example above. Then add 1.
DATEDIFF(day, '16 Dec 2008 10:10', '16 Dec 2008 10:50') + 1

Related

How to get the day of the week from a time stamp?

I am using Hive .14 to analyze the following input data in timestamp format (# is irrelevant to the explanation):
#
Datetime
1
2022-03-01 00:13:08
2
2022-03-31 23:52:24
3
2022-02-28 23:32:40
and I want to get what day of the week in which each data took place (either by a number representing the day from 0 to 6, or the day itself) in, similar to the next format:
#
Day of the week
1
Tuesday or 2
2
Thursday or 4
3
Monday or 1
I have tried to use the unixtime command to transform the timestamp into an integer
like this:
select cast(from_unixtime(unix_timestamp(datetime,'yyyy-MM-dd'),'yyyyMMdd') as int) as dayint from yellowtaxi;
To later use the from_unixtime(dayint,u) query to get the day of the week in which it happened, however, this results in all the days from all the rows being equal to 20220301 and to all the days being equal to 7 when using from_unixtime(dayint,u).
What am I doing wrong, or is there an easier way to do it?
I have already tried the day_format() and the dayofweek() queries, but none of them seem to be available in my hive version.

Why is Redshift datediff giving different weeks when it is the same number of day difference?

I'm trying to find the number of weeks between two days. When the difference is 8 days, I should be getting 1 or 2 weeks, depending on how the function works in Redshift (rounds up or down). However, it should be consistent whichever way it chooses.
I realize that I could simply take the number of days and then divide by 7 and do either a ROUND or a CEIL, but I am simply trying to understand why DATEDIFF(weeks, date1, date2) provides either 1 or 2 when I have the two dates different by 8 days.
SELECT
DATEDIFF(weeks, '2019-03-17', '2019-03-25') AS week_difference1,
DATEDIFF(days, '2019-03-17', '2019-03-25') AS day_difference1,
DATEDIFF(weeks, '2019-03-16', '2019-03-24') AS week_difference2,
DATEDIFF(days, '2019-03-16', '2019-03-24') AS day_difference2
Result:
week_difference1 = 1
day_difference1 = 8
week_difference2 = 2
day_difference2 = 8
As with many software products from the US, the first day of the week in Redshift (at least far as DATEDIFF is concerned) is Sunday, and not the ISO standard of Monday. Therefore when calculating the number of weeks between two dates the boundary is Saturday/Sunday.
In your example, the eight days between the 16th March 2019 and 24th March 2019 crosses two week boundaries (one on 16/17 March and one on 23/24 March), so the resulting DATEDIFF value is 2 (two week boundaries crossed).
However, the eight days between the 17th March and 25th March only crosses one week boundary (23/24 March) so the resulting DATEDIFF value is 1.

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.

Select records from after last 6 AM

Have table: item (int) and timestamp (datetime).
Need to know if there are any records with timestamp after last 6 AM.
Example:
At 5 AM it should check if there are any records from after 6 AM
yesterday. AT 7 AM it should check if there are any records from
after 6 AM today
This could be done of course by making a variable with datepart as:
if time now is < 6 AM datepart should be yesterday if time now
is >= 6 AM datepart should be today
but there must be a simpler way ?
This expression should always return the most recent 06:00 in the past:
select DATEADD(hour,
(DATEDIFF(hour,'2014-01-01T06:00:00',CURRENT_TIMESTAMP)/24)*24,
'2014-01-01T06:00:00')
It works by asking how many hours have happened since some arbitrary, known, 6AM. We then round this number down to the closest multiple of 24 (by dividing and multiplying with integer maths), and add this number back onto the same, arbitrary, 6AM

DateDiff SQL Server Problem

Could some one please explain this to me as I am a touch confused as to why this is happening? Basically what I would like to know is why there is a difference between Sundays date and every other day of this week in weeks from the year 0. If that makes sense!
I tried setting the date first but this had no effect. Surely Monday – Sunday of this week should all have the same difference in weeks from the year zero?
Set Datefirst 1
Select DateName(dw,0) --Monday
Select DateDiff(week, 0, '20091109')--Monday: Difference 5732
Select DateDiff(week, 0, '20091114')--Saturday: Difference 5732
Select DateDiff(week, 0, '20091115')--Sunday: Difference 5733
What makes this even more bizarre is if you take the same two dates and date diff them you get one week for the one and 6 days for the other. Am I missing something here?
Select DateDiff(dd,'20091109','20091115')--6 Days difference
Select DateDiff(ww,'20091109','20091115')--1 Week difference
I am using SQL Server 2005
Sunday is considered the first day of the week in a number of countries.
All the datediff functions do is count the number of date boundarys between the two datetime arguments passed to it. So if a week is defined to start Sunday, then the week boundary is midnight Sunday Morning.
So, From 11:59 Saturday night to 00:01 am Sunday Morning, will be the same datediff(week, x, y) as from 00:01 Sunday to 11:59 PM Saturday Night - 13 days later.
x ------------------------------x ==> 1 week diff
| Su M T W T F S | Su M T W T F S |
| | |
x-x ==> Also 1 week diff
I think the problem you are facing is the boundary dates. Are they inclusive?
Select DateDiff(dd,'20091109','20091115')--6 Days difference
Select DateDiff(dd,'20091109','20091116')--7 Days difference
DateDiff - Returns the number of date and time boundaries crossed between two specified dates. (Books Online Definition)
If you Set Datefirst = 1, Monday is the first day of the week, and the week calculations would use MOD 7 for number of days in the week and /7 for week count, in which sunday would be used as 1, and the others FLOORed to 0.