weekBegin
Syntax
weekBegin(X, [weekday=0], [offset], [n=1])
Details
For each element of X, return the first date of the week that it belongs to and that starts on the day as specified by parameter weekday.
-
If parameter weekday>weekday(X, false): for each element of X, return a date that corresponds to the specified weekday parameter in the previous calendar week.
-
If parameter weekday<=weekday(X, false): for each element of X, return a date that corresponds to the specified weekday parameter in the same calendar week.
If parameter offset is specified, the result is updated every n weeks. Please refer to example 2 below. The parameters offset and n must be specified together, and offset takes effect only when n > 1.
Parameters
X is a scalar/vector of type DATE, DATETIME, DATEHOUR, TIMESTAMP or NANOTIMESTAMP.
weekday (optional) is an integer from 0 to 6. 0 means Monday, 1 means Tuesday, ..., and 6 means Sunday. The default value is 0.
offset (optional) is a scalar of the same data type as X. It must be no greater than the minimum value of X. The default value is the minimum value of X.
n (optional) is a positive integer. The default value is 1.
Returns
A scalar or vector of the DATE type.
Examples
Example 1
t = table(2017.12.01..2017.12.14 as date);
update t set weekday=weekday(date, false), weekBegin=weekBegin(date), weekBegin4=weekBegin(date,4);
t;
| date | weekday | weekBegin | weekBegin4 |
|---|---|---|---|
| 2017.12.01 | 4 | 2017.11.27 | 2017.12.01 |
| 2017.12.02 | 5 | 2017.11.27 | 2017.12.01 |
| 2017.12.03 | 6 | 2017.11.27 | 2017.12.01 |
| 2017.12.04 | 0 | 2017.12.04 | 2017.12.01 |
| 2017.12.05 | 1 | 2017.12.04 | 2017.12.01 |
| 2017.12.06 | 2 | 2017.12.04 | 2017.12.01 |
| 2017.12.07 | 3 | 2017.12.04 | 2017.12.01 |
| 2017.12.08 | 4 | 2017.12.04 | 2017.12.08 |
| 2017.12.09 | 5 | 2017.12.04 | 2017.12.08 |
| 2017.12.10 | 6 | 2017.12.04 | 2017.12.08 |
| 2017.12.11 | 0 | 2017.12.11 | 2017.12.08 |
| 2017.12.12 | 1 | 2017.12.11 | 2017.12.08 |
| 2017.12.13 | 2 | 2017.12.11 | 2017.12.08 |
| 2017.12.14 | 3 | 2017.12.11 | 2017.12.08 |
Example 2
t = table(2018.01.03+0..10*3 as date, 0..10 as x);
update t set weekday=weekday(date, false), weekBegin=weekBegin(date,,2018.01.02,2);
t;
| date | x | weekday | weekBegin |
|---|---|---|---|
| 2018.01.03 | 0 | 2 | 2018.01.01 |
| 2018.01.06 | 1 | 5 | 2018.01.01 |
| 2018.01.09 | 2 | 1 | 2018.01.01 |
| 2018.01.12 | 3 | 4 | 2018.01.01 |
| 2018.01.15 | 4 | 0 | 2018.01.15 |
| 2018.01.18 | 5 | 3 | 2018.01.15 |
| 2018.01.21 | 6 | 6 | 2018.01.15 |
| 2018.01.24 | 7 | 2 | 2018.01.15 |
| 2018.01.27 | 8 | 5 | 2018.01.15 |
| 2018.01.30 | 9 | 1 | 2018.01.29 |
| 2018.02.02 | 10 | 4 | 2018.01.29 |
Example 3
date=2012.10.02 2012.10.03 2012.10.07 2012.10.08 2012.10.12 2012.10.16 2012.10.18 2012.10.20 2012.10.25 2012.10.28
time=[09:34:07,09:36:42,09:36:51,09:36:59,09:32:47,09:35:26,09:34:16,09:34:26,09:38:12,09:38:13]
sym = take(`MSFT,10)
price= 49.6 29.46 29.52 30.02 174.97 175.23 50.76 50.32 51.29 52.38
qty = 2200 1900 2100 3200 6800 5400 1300 2500 8800 4500
t1 = table(date, time, sym, qty, price);
t1;
| date | time | sym | qty | price |
|---|---|---|---|---|
| 2012.10.02 | 09:34:07 | MSFT | 2200 | 49.6 |
| 2012.10.03 | 09:36:42 | MSFT | 1900 | 29.46 |
| 2012.10.07 | 09:36:51 | MSFT | 2100 | 29.52 |
| 2012.10.08 | 09:36:59 | MSFT | 3200 | 30.02 |
| 2012.10.12 | 09:32:47 | MSFT | 6800 | 174.97 |
| 2012.10.16 | 09:35:26 | MSFT | 5400 | 175.23 |
| 2012.10.18 | 09:34:16 | MSFT | 1300 | 50.76 |
| 2012.10.20 | 09:34:26 | MSFT | 2500 | 50.32 |
| 2012.10.25 | 09:38:12 | MSFT | 8800 | 51.29 |
| 2012.10.28 | 09:38:13 | MSFT | 4500 | 52.38 |
select avg(price),sum(qty) from t1 group by weekBegin(date, 4, 2012.10.01, 2);
| weekBegin_date | avg_price | sum_qty |
|---|---|---|
| 2012.09.28 | 34.65 | 9400 |
| 2012.10.12 | 100.514 | 24800 |
| 2012.10.26 | 52.38 | 4500 |
Related function: weekEnd
