I am trying to generate a dates array in BQ with dates that lie within several start and end dates.
For example, for days between one start and end date it looks like this:
SET DATES = GENERATE_DATE_ARRAY(DATE(2020,02,01), DATE(2022, 04, 25), INTERVAL 1 WEEK);
But how would I generate DATES if I want all these dates included: event_date BETWEEN "2020-02-01" AND "2020-04-25", event_date BETWEEN "2021-02-01" AND "2021-04-25", event_date BETWEEN "2022-02-01" AND "2022-04-25")
I didn't come across any easy fix.
Consider below
with dates_ranges as (
select "2020-02-01" start_date, "2020-04-25" end_date union all
select "2021-02-01", "2021-04-25" union all
select "2022-02-01", "2022-04-25"
)
select date
from dates_ranges,
unnest(generate_date_array(date(start_date), date(end_date), interval 1 week)) date
You can just create all those separate arrays and then concat_array() them, no?
Related
I have a table full of daily aggregate data, but I occasionally need to pull weekly aggregate data, and provide info on increases or decreases. For that reason, I was considering using T-SQL DATEPART functionality to get week-number and year info for dates.
For example, I can get the following info using today's date (9/11/2020):
#nowWeekNumber int = datepart(wk,#today), --yields 37
#nowYear int = datepart(year,#today), --yields 2020
Using that logic, I could then gather info on records where year is 2020 and weekNumber is 36, and then I could compare those numbers to get a weekly increase/decrease. (Or maybe I'd compare weeks 35 and 36 to ensure that I'm dealing w/ entire weeks, but you get the picture)
However, if the date is 2021-01-03, that's going to return a year of 2021, and a weekNumber of 2. If I subtract a week, I'm going to get year 2021 and weekNumber 1. That weekNumber is only going to contain January 1st and 2nd, because 12/27 thru 12/31 are considered year 2020 and weekNumber 53 (even though the calendar week is 12/27 thru 1/2).
In other words, I don't think I can use weekNumber to gather weekly data, even though that would be fairly convenient. I'm aware that I can use DATEADD functions to grab the start and end-date for consecutive weeks, and I can then gather aggregate data for records BETWEEN those dates, but is there a more-convenient way to do this?
Why don't you consider using dateDiff as key function? As...
select dateDiff(wk, 0, getDate())
Returns a single integer for the whole week (6297 for '20200911') and :
select dateAdd(wk, dateDiff(wk, 0, getDate()), 0),
dateAdd(dd, 6, dateAdd(wk, dateDiff(wk, 0, getDate()), 0))
or
select dateAdd(wk, 6297, 0),
dateAdd(dd, 6, dateAdd(wk, 6297, 0))
gives you the 1st and last day of that week.
You can use DATEPART but instead of wk you can use the iso week. Then you don't have the problem with a week being split in 2. To be sure also use SET DATEFIRST to define exactly on which day the week starts.
SET DATEFIRST 1; --use monday as first day of the week
SELECT datepart(iso_week,'2021-01-01');
SELECT datepart(iso_week,'2021-01-03');
SELECT datepart(iso_week,'2021-01-04');
The other option is to create your own calendar table and join that to your daily table.
EDIT: for a week start on sunday
SET DATEFIRST 7;
SELECT DATEPART(WEEK, DATEADD( DAY, 1-DATEPART(WEEKDAY,'2020-12-27'),'2020-12-27' ) )
SELECT DATEPART(WEEK, DATEADD( DAY, 1-DATEPART(WEEKDAY,'2020-12-28'),'2020-12-28' ) )
SELECT DATEPART(WEEK, DATEADD( DAY, 1-DATEPART(WEEKDAY,'2021-01-01'),'2021-01-01' ) )
SELECT DATEPART(WEEK, DATEADD( DAY, 1-DATEPART(WEEKDAY,'2021-01-02'),'2021-01-02' ) )
I am trying to create a report and the view considers monthly ranges. The report has different views (daily, weekly, monthly). I have a procedure that brings the records but I am having issues trying to build the report with dates in which I have no records.
For instance, if I run the report with the daily view on Dec 19th, I will only see columns until the 19th. I want to see columns until the 31th (end of month) even if there are no records.
I attached a pic of how it currently looks. I was also trying to do a FULL JOIN with a variable table that I have that contains all the day of the month. I also use a similar approach with the weekly view but with the week range. It still doesn't work with this view, and I'd like it to work.
With the weekly report it happens something similar: if I run the report today, the last week of the month won't show on the report (because there are no records) and I'd like it to be shown.
I hope it's clear enough.
As Sean suggested in his comment, you can use a calendar type table and then can use left join form that table to your data table to display all rows including one with no data etc.
For calendar table, you can also use following code with your choice of date range:
DECLARE #Date1 DATE= '20171201';
DECLARE #Date2 DATE= '20171215';
SELECT
DATEADD(DAY, number, #Date1) AS [Date],
DATEPART(DW, DATEADD(DAY, number, #Date1)) DayOfWeek,
DATEPART(DY, DATEADD(DAY, number, #Date1)) DayOfYear,
DATEPART(day, DATEDIFF(day, 0, DATEADD(DAY, number, #Date1)) / 7 * 7) / 7 + 1 AS WeekOfMonth,
DATEPART(WEEK, DATEADD(DAY, number, #Date1)) WeekOfYear
FROM master..spt_values
WHERE type = 'P'
AND DATEADD(DAY, number, #Date1) <= #Date2;
Resulting:
Hope this help?
Good luck.
As the title says, can I create a new column from two fields? For example, I created a new column from the master values table to represent the months, and I have another column with the year. Can I combine them to create a date with whatever day? What I have is on the left, what I want is on the right:
Month Year NewDate
---- ---- -------
3 2015 2015-03-01
4 2015 2015-04-01
Whenever I tried to cast the month as datetime, it would look like this:
1900-01-03
This is probably a dumb question, but I created a timeline that lists out the months, and the trouble lies whenever the end date parameter chosen is less than the start date parameter, such as December 2015 to January 2016. I created the report in SSRS and the parameters aren't quite working correctly. Thanks guys!
Also, here's a snapshot of what the timeline looks like:
SELECT
DATEFROMPARTS ( [Year], [Month], 1 ) AS NewDate
FROM
YourTable
;
select dateadd(mm, t.Month -1, dateadd(yy,t.Year-1900,'19000101'))
from table1 t
I am trying to add days to the current date and it's working fine but when I add 360 days to the current date it gives me wrong value.
eg: Current Date is 11/04/2014
And I am adding 360 Days to it, it should give me 11/04/2015, but it is showing the same date 11/04/2014. the year is not changing.
Here is my code:
select dateadd(dd,360,getdate())
Just do-
Select (Getdate()+360) As MyDate
There is no need to use dateadd function for adding or subtracting days from a given date. For adding years, months, hours you need the dateadd function.
select dateadd(dd,360,getdate()) will give you correct date as shown below:
2017-09-30 15:40:37.260
I just ran the query and checked:
Dateadd(datepart,number,date)
You should use it like this:
select DATEADD(day,360,getdate())
Then you will find the same date but different year.
From the SQL Server 2017 official documentation:
SELECT DATEADD(day, 360, GETDATE());
If you would like to remove the time part of the GETDATE function, you can do:
SELECT DATEADD(day, 360, CAST(GETDATE() AS DATE));
In SQL Server 2008 and above just do this:
SELECT DATEADD(day, 1, Getdate()) AS DateAdd;
can try this
select (CONVERT(VARCHAR(10),GETDATE()+360,110)) as Date_Result
Two or three ways (depends what you want), say we are at Current Date is
(in tsql code) -
DECLARE #myCurrentDate datetime = '11Apr2014 10:02:25 AM'
(BTW - did you mean 11April2014 or 04Nov2014 in your original post? hard to tell, as datetime is culture biased. In Israel 11/04/2015 means 11April2014. I know in the USA 11/04/2014 it means 04Nov2014. tommatoes tomatos I guess)
SELECT #myCurrentDate + 360 - by default datetime calculations followed by + (some integer), just add that in days. So you would get 2015-04-06 10:02:25.000 - not exactly what you wanted, but rather just a ball park figure for a close date next year.
SELECT DateADD(DAY, 365, #myCurrentDate) or DateADD(dd, 365, #myCurrentDate)
will give you '2015-04-11 10:02:25.000'. These two are syntatic sugar (exacly the same). This is what you wanted, I should think. But it's still wrong, because if the date was a "3 out of 4" year (say DECLARE #myCurrentDate datetime = '11Apr2011 10:02:25 AM') you would get '2012-04-10 10:02:25.000'. because 2012 had 366 days, remember? (29Feb2012 consumes an "extra" day. Almost every fourth year has 29Feb).
So what I think you meant was
SELECT DateADD(year, 1, #myCurrentDate)
which gives 2015-04-11 10:02:25.000.
or better yet
SELECT DateADD(year, 1, DateADD(day, DateDiff(day, 0, #myCurrentDate), 0))
which gives you 2015-04-11 00:00:00.000 (because datetime also has time, right?). Subtle, ah?
This will give total number of days including today in the current month.
select day(getDate())
Add Days in Date in SQL
DECLARE #NEWDOB DATE=null
SET #NEWDOB= (SELECT DOB, DATEADD(dd,45,DOB)AS NEWDOB FROM tbl_Employees)
SELECT DateAdd(5,day(getdate()) this is for adding 5 days to current days.
for eg:today date is 23/08/2018 it became 28/08/2018 by using the above query
I can get a single count row from a specified date range like this:
SELECT table.[EVENT NAME], Count(*) AS [Count]
FROM table
WHERE [EVENT]='alphabetical' And table.DATE>=#11/20/2010# And (table.DATE)<=#11/26/2010#
GROUP BY table.[EVENT NAME];
but how could I add multiple rows with different date ranges?
[EVENT NAME],[DATE 11/20-11/26],[DATE 11/27-12/3], etc...
EDIT
the data would look something like this
event1;1/11/2010
event1;1/11/2010
event2;1/11/2010
event2;1/11/2010
event2;1/11/2010
event3;1/11/2010
event1;1/12/2010
event1;1/12/2010
event2;1/12/2010
event2;1/12/2010
event4;1/12/2010
event4;1/12/2010
etc.
and would like something like this (preferably with more columns) :
event1;2;2
event2;3;2
event3;1;0
event4;0;2
You'd use a group by clause and group by the date.
You didn't provide example records with expected results, that helps us help you :).
In other words post more information..
But from what I can tell you want a count based on a date range.
So if you had 1/1/2010 with 10 rows
and 1/2/2010 with 20 referenced rows
and 1/3/2010 with 6 reference rows...you'd want output like this:
1/1/2010 10
1/2/2010 20
1/3/2010 6
So SELECT COUNT(*), MyDate FROM MyTable GROUP BY MyDate
To answer your question about a date range, think of how group by works, it works by grouping a set of data by combining all sets that match a criteria. So when you say group by date it groups by a single date. You want a date range, so each row should know about or understand a range (Start to End). So you need to include these columns in each of your rows by generating them via SQL.
Edit
For instance
SELECT Count(*), DATEADD(day, -10, GetDate()) AS StartDate, DATEADD(day, 10, GetDate()) AS EndDate
FROM MyTable GROUP BY StartDate, EndDate
Access has similiar functions to add days to dates so look that up for MS Access. Then just generate a start and end date for each column.