Ive got two fields that calculate the week number of two dates start date and end date. So I have two field called FirstWeek and LastWeek. I want to find all the numbers between the FirstWeek and LastWeek and put those in another field.
For instance firstweek = 35
lastweek = 39
I want field weeks= 35, 36, 37, 38, 39
Any thoughts would be greatly appreciated. I would like to use formula if at all possible.
In #Formula language you also have For loop. If you have number field firstweek and number field lastweek you can create number field weeks, my advice is that this is a multivalue field, and try this formula:
REM {firstweek_date is the date field from which you are calculating the value of firstweek number};
year_firstweek := #Year(firstweek_date);
REM {lastweek_date is the date field from which you are calculating the value of lastweek number};
year_lastweek :=#Year(lastweek_date);
REM {if the year diff is one then the first value of weeks field is first week};
REM {NOTE: I took that every year has 52 weeks.};
REM {If this is not OK then for number_of_weeks_in_year set the value in a same way as you are doing with firstweek but for date #Date(year_firstweek ; 12 ; 31 ).};
REM {If the dates are in same Year then as written before you can use the same logic};
REM {Otherwise I set field week to 0};
#If((year_lastweek-year_firstweek) = 1;
#Do(
FIELD weeks := firstweek;
number_of_weeks_in_year := 52;
#For(n := firstweek+1; n <= number_of_weeks_in_year; n := n + 1;FIELD weeks := weeks:n);
#For(n := 1; n <= lastweek; n := n + 1;FIELD weeks := weeks:n)
);
(year_lastweek-year_firstweek) = 0;
#Do(
FIELD weeks := firstweek;
#For(n := firstweek+1; n <= lastweek; n := n + 1;FIELD weeks := weeks:n)
);
FIELD weeks := 0
)
Related
I am working with data output from a program and then uploaded into google sheets that looks like the following:
So my timestamp is in the format of 1/2/2020 8:56 (and I totally get that query has to work with yyyy-mm-dd format, which is why we have to do the acrobatics)
I'm using the query function to pull needed data into different tabs and would like to use the Today() or Now() function to only pull the last 12 or 24 hours results and I can't seem to get it to work. (Mind that I'm just learning the query function)
So I have
=query(RawDataUpload!A:I,"select * where A is not null and A >= date '2020-01-02' and B = 'Buying' and H > 0 and H < 50000 Order by D, H desc")
and it works ok, but I have to put in the date each new day and at 8am it's only 8 hours of data instead of 12 (more of a problem at 2am)
I've tried using a few examples and I keep getting a parsing error or some error
=query(RawDataUpload!A:I,"select * where A is not null and A >= date '"&TEXT(TODAY(),"yyyy-mm-dd")&"'",1 and B = 'Buying' and H > 0 and H < 50000 Order by D, H desc")")
=query(RawDataUpload!A:I,"select * where A is not null and A >= date '"&TEXT(TODAY(),"yyyy-mm-dd")&"'" and B = 'Buying' and H > 0 and H < 50000 Order by D, H desc")")
[I see & thrown in but no explanation of why or what it does, and same to the " " instead of ' ' and why the mix mash of using both in the examples of using today() and I've found 0 examples of using the now instead of the date function in my googling.]
So is there a way to limit by date (and possibly time) using the today() or Now()-12 function embedded in the Query function in google sheets?
Try
=query(RawDataUpload!A:I,"select * where A is not null and A >= date '"&TEXT(TODAY(),"yyyy-mm-dd")&"' and B = 'Buying' and H > 0 and H < 50000 Order by D, H desc")
The & is used to join a string to form a formula.
For example, if you want the formula to read:
... and A >= date '2020-01-03' and B =....
But you'd like the date to be today's date, you would use:
...and A >= date '"&TEXT(TODAY(),"yyyy-mm-dd")&"' and B =...
The " is used to exit the query string and add the Text() formula. And the & is used to join them.
You can use Filter like this:
= filter ( A2:I,B2:B="Buying",
text(A2:A,"yyyy.mm.dd.hh")>=text(now(),"yyyy.mm.dd.") & "08",
text(A2:A,"yyyy.mm.dd.hh")<=text(now()+0,"yyyy.mm.dd.") & "16"
)
begin from row 2 because in row 1 are column titles
Remarks1 : 08 & 16 are for limit hour begin from 08 and last 16, can be changed
Remarks2 : +0 only this day, if change to +1 this day and tomorrow and so on
I need a help on this function. The code has no error but it keeps returning the same result, that the value from of the second condition/statement.
This is how it suppose to work:
If employees age at hire date (DOFA) is less than or equals to 25 the retirement date is 35 years from hire date. Otherwise retirement date is when employees age is 60
create or replace function EDOR_FUNCTION
(DOFA in date, DOB in date)
return date
is
new_edor_date date;
begin
if
DOFA - DOB <= 25 then new_edor_date := add_months(DOFA, 35*12);
else
new_edor_date := add_months(DOB, 60*12);
end if;
return new_edor_date;
end;
Your condition subtracts one date from another. This gives the number of days between the two, not the number of years.
months_between() gives the number of months between two dates. Multiply by twelve to get number of years
if months_between(DOFA , DOB) <= (25*12) then
new_edor_date := add_months(DOFA, 35*12);
else
new_edor_date := add_months(DOB, 60*12);
end if;
Given the start day (Wednesday = 4), and the number of days in a month (31), what is an elegant way to find the number of week rows a calendar of the month would require?
For the current month (startDay = 4, daysInMonth = 31), it would be 5. But if daysInMonth = 33, it would be 6 rows.
This doesn't quite work:
int numRows = (startDay+daysInMonth)/daysInWeek;
if ((startDay+daysInMonth) % daysInWeek != 0) {
numRows++;
}
Just change to
int numRows = (startDay + daysInMonth - 1) / daysInWeek;
if ((startDay+daysInMonth - 1) % daysInWeek != 0) {
numRows++;
}
and you should get the correct result.
EDIT: just to slightly expand on it : you have the right idea, you just forgot to account for the fact that the offset for day 1 is 0, not 1.
Actually, I think your original algorithm is correct, just need to subtract 1 when doing modulo daysInWeek.
daysInWeek = 7
startDay = 3 # Zero based day of week array, 3 = Wednesday
daysInMonth = 31
numRows = (startDay+daysInMonth)/daysInWeek
if ((startDay+daysInMonth - 1) % daysInWeek != 0)
numRows += 1
end
print numRows
It shows 6 correctly. (BTW, why do you need a month with 33 days?) It should be 6 rows for a 33 day month (if there was such a thing).
int temp = daysInMonth;
temp = temp - (7 - startDay);
int result = ceiling(temp / 7) + 1;
Here is a generic way to do it in C#, which works by counting the Saturdays in a month and then adding one if there's any left over days. Since it literally reads a calendar like a human would, there's no strange calendar arithmetic needed. It's all offloaded to the C# DateTime code, we just piggyback off that.
I chose Saturday because most calendars go Sunday (far left) to Saturday (far right). You can just choose a different day if you wish to denote the end of a week.
public static int RowsForMonth(int year, int month)
{
int days = DateTime.DaysInMonth(year, month);
int rows = 0;
int i = 0;
while(i < days)
{
i++;
DateTime date = new DateTime(year, month, i);
if (date.DayOfWeek == DayOfWeek.Saturday || i == days)
rows++;
}
return rows;
}
Jan 2022 -> 6
Feb 2022 -> 5
Mar 2022 -> 5
The Problem: Given a day of the week (1, 2, 3, 4, 5, 6, 7), a starting date and an ending date, compute the number of times the given day of the week appears between the starting and ending dates not inclusive of a date for which there were no sales.
Context:
Table "Ticket" has the following structure and sample content:
i_ticket_id c_items_total dt_create_time dt_close_time
----------------------------------------------------------------------------
1 8.50 '10/1/2012 10:23:00' '10/1/2012 11:05:05'
2 10.50 '10/1/2012 11:00:00' '10/1/2012 11:45:05'
3 8.50 '10/2/2012 08:00:00' '10/2/2012 09:25:05'
4 8.50 '10/4/2012 08:00:00' '10/4/2012 09:25:05'
5 7.50 '10/5/2012 13:22:23' '10/5/2012 14:33:27'
.
.
233 6.75 '10/31/2012 23:20:00' '10/31/2012 23:55:39'
Details
There may or may not be any tickets for one or more days during a month. (i.e. the place was closed that/those day/s)
Days in which the business is closed are not regular. There is no predictable pattern.
Based on Get number of weekdays (Sundays, Mondays, Tuesdays) between two dates SQL,
I have derived a query which returns the number of times a given day of the week occurs between the start date and the end date:
DECLARE #dtStart DATETIME = '10/1/2013 04:00:00'
DECLARE #dtEnd DATETIME = '11/1/2013 03:59:00'
DECLARE #day_number INTEGER = 1
DECLARE #numdays INTEGER
SET #numdays = (SELECT 1 + DATEDIFF(wk, #dtStart, #dtEnd)-
CASE WHEN DATEPART(weekday, #dtStart) #day_number THEN 1 ELSE 0 END -
CASE WHEN DATEPART(weekday, #dtEnd) <= #day_number THEN 1 ELSE 0 END)
Now I just need to filter this so that any zero-dollar days are not included in the count. Any help you can provide to add this filter based on the contents of the tickets table is greatly appreciated!
If I understand correctly, you can use a calendar table to count the number of days where the day of week is n and between the start and end and is a date that has ticket sales, which I guess is when the date exists in tickets and has the sum(c_items_total) > 0
WITH cal AS
(
SELECT cast('2012-01-01' AS DATE) dt, datepart(weekday, '2012-01-01') dow
UNION ALL
SELECT dateadd(day, 1, dt), datepart(weekday, dateadd(day, 1, dt))
FROM cal
WHERE dt < getdate()
)
SELECT COUNT(1)
FROM cal
WHERE dow = 5
AND dt BETWEEN '2012-04-01' AND '2012-12-31'
AND EXISTS (
SELECT 1
FROM tickets
WHERE cast(dt_create_time AS DATE) = dt
GROUP BY cast(dt_create_time AS DATE)
HAVING sum(c_items_total) > 0
)
OPTION (MAXRECURSION 0)
SQLFiddle
can i use to_number(to_char()) function in order to exclude all the weekends from a range of dates?
For instance, there are two date columns in my table such as start and finish (in the form of '06/06/2011 10:00:00 am'), and i want to estimate the duration of finish-start excluding Saturdays and Sundays.
If I understand you right you want to calculate the difference between two dates, but exclude the 2 days of each weekend in the range from the result. Is that correct?
If this is what you want the below code should work with the following assumptions:
I am assume start and end will not be on weekends.
I am not validating that end is before start.
Basically its just a matter of working out how many weekends are in the date range. So obviously there's one weekend per 7 days. Then we just have to check if the range wraps around a weekend, and if so add one more.
FUNCION dateDiff( dt_start DATE, dt_end DATE ) RETURN NUMBER
IS
raw_diff NUMBER;
weekends NUMBER;
BEGIN
raw_diff := dt_end - dt_start;
weekends := TRUNC( raw_diff / 7 );
IF( ( dt_start - TRUNC( dt_start, 'DAY' ) )
> ( dt_end - TRUNC( dt_end , 'DAY' ) ) )
THEN
weekends := weekends + 1;
END IF;
RETURN raw_diff - ( weekends * 2 );
END;