How to calculate the number of weeks between two dates in hive ? I need a better approach.. As of now I am calculating by datediff function. is this the correct approach ?
datediff(String EndDate, String StartDate) / 7
Related
I have approximately the same table (excluding count column). I want to calculate the number of working days (Mon-Fri) and exclude public holidays.
I tried to try the following query
SELECT count(distinct(date)) from MYDB where dummy <> 1
However, it gives the only total number of days including weekends. Additionally, if use this command it counts distinct dates, however, my dates do not show a full month, so another logic should've used. Could you help to figure out which code is better to use?
there should be a function in Vertica that extracts weekday from date, so to exclude weekends you'll need to add another condition like
extract(dow from date) not in (6,0)
(6 is Sat, 0 is Sun in this case)
For example, if I have a data set including two columns, one which shows the month as a number and the other which shows the year (result of grouping my data using GROUP BY), I want to add another column called 'Days in the month' which will display the number of days in the respective month. Is there a way I can do this? Is there some function I can add in the SELECT clause?
I want to do this since there are further calculations I need to do with that number for each row.
In SQL Server 2012+, you can use:
select day(eomonth(datecol))
eomonth() gets the last day of the month. day() just returns the day of the month -- the number of days in the month, in this case.
For older SQL Server versions, I use the following:
DAY(DATEADD(MONTH, DATEDIFF(MONTH, -1, date_column)- 1, -1))
Much less elegant than the previous answer, but functional.
When using Hive there is the functionality to return the select datediff('date1','date2') from table value which returns the number of days between two dates.
What if I would be interested in the months between the two dates?
One option would be to divide the result by 30.5, as the average months has approximately 30.5 days, but this would certainly yield an inprecision when large date ranges are considered.
Do you know a way of retrieving the number of months (rather than the number of days) in a similar fashion with standard SQL (ideally HIVE) syntax?
You can try with:
SELECT CAST(MONTHS_BETWEEN(date1, date2) AS INT) as numberOfMonthsBetweenDates
FROM table
This will return your expected result.
How to get the difference between two dates (informix) in integer format like that
day = 15
mon = 2
year = 1
There are two sets of date/time values in Informix: DATE and DATETIME.
The DATE type is oldest (it was in the precursor to the SQL-based Informix), and represents the integer number of days since a reference date (where day 0 is 1899-12-31, so day 1 was 1900-01-01).
You get the difference between two DATE values in days by subtracting one from the other.
The DATETIME system is newer (but still old — circa 1990). You can take the difference between two DATETIME YEAR TO DAY values and get a result that is an INTERVAL DAY TO DAY (essentially the number of days).
You could also take the difference between two DATETIME YEAR TO MONTH values and get a result that is an INTERVAL YEAR TO MONTH.
However, there is no way to get a difference in years, months and days because there is no simple way to deduce that value. In fact, ISO SQL recognizes two classes of INTERVAL: those in the YEAR-MONTH group, and those in the DAY-SECOND group. You can't have an INTERVAL that crosses the MONTH/DAY barrier.
Use the MDY function :
select mdy(2,15,2014) - mdy(1,15,2014) from sysmaster:sysdual
I'm trying to do something that seems simple but I can't figure out how to write it up in SQL. I have a table of records including a field containing a date. I would like to get an overall average number of records per month using that date field. This is not an AVG that is grouped by month, but an overall average.
So if my table contains quotes, and there is 7 different months of data in that table, I'm looking to get:
Total number of records / n months of data
Now I need to get this into SQL.
You can try to use the DateDiff function on the Min and Max of your date field.
DateDiff function:
DateDiff ( interval, date1, date2, [firstdayofweek], [firstweekofyear])
Example:
SELECT count(*) / DateDiff('m', Min(date), Max(date))
FROM ...
How does this work?
count will give you the total number of rows (# of quotes in your quotes table), and DateDiff will give you the total number of months between the first and last date.