Skip to content

Commit b1410dd

Browse files
committed
[WIP] Document date/time function additions to the SQL API for Analytics
1 parent fd84924 commit b1410dd

File tree

2 files changed

+274
-0
lines changed

2 files changed

+274
-0
lines changed

src/content/docs/analytics/analytics-engine/sql-reference/aggregate-functions.mdx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,57 @@ SELECT topKWeighted(double1, _sample_interval) FROM my_dataset
248248
-- find the 15 most common values of <blob1>, weighted by `_sample_interval`
249249
SELECT topKWeighted(15)(blob1, _sample_interval) FROM my_dataset
250250
```
251+
252+
## countIf
253+
254+
Usage:
255+
256+
```sql
257+
countIf(<expr>)
258+
```
259+
260+
`countIf` is an aggregation function that returns the number of rows in the results set,
261+
but only counting rows where a provided expression evaluates to true.
262+
263+
Example:
264+
265+
```sql
266+
-- return the number of rows where `double1` is greater than 5
267+
count(double1 > 5)
268+
```
269+
270+
## sumIf
271+
272+
Usage:
273+
274+
```sql
275+
sumIf(<expr>, <expr>)
276+
```
277+
278+
`sumIf` is an aggregation function that returns the sum of a first expression across all rows in the results set,
279+
but only including rows where a second expression evaluates to true.
280+
281+
Example:
282+
283+
```sql
284+
-- return the sum of column `item_cost` of all items where another column `in_stock` is not zero
285+
sumIf(item_cost, in_stock > 0)
286+
```
287+
288+
## avgIf
289+
290+
Usage:
291+
292+
```sql
293+
avgIf(<expr>, <expr>)
294+
```
295+
296+
`avgIf` is an aggregation function that returns the mean of an expression across all rows in the results set,
297+
but only including rows where a second expression evaluates to true.
298+
299+
Example:
300+
301+
```sql
302+
-- return the mean of column `item_cost` where another column `in_stock` is not zero
303+
avg(item_cost, in_stock > 0)
304+
```

src/content/docs/analytics/analytics-engine/sql-reference/date-time-functions.mdx

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,223 @@ FROM your_dataset
119119
GROUP BY hour
120120
ORDER BY hour ASC
121121
```
122+
123+
## toYear
124+
125+
Usage:
126+
127+
```sql
128+
toYear(<datetime>)
129+
```
130+
131+
`toYear` returns the year of a datetime.
132+
133+
Examples:
134+
135+
```sql
136+
-- returns the number 2025
137+
toYear(toDateTime('2025-10-27 00:00:00'))
138+
```
139+
140+
## toMonth
141+
142+
Usage:
143+
144+
```sql
145+
toMonth(<datetime>)
146+
```
147+
148+
`toMonth` returns the year of a datetime.
149+
150+
Examples:
151+
152+
```sql
153+
-- returns the number 10
154+
toMonth(toDateTime('2025-10-27 00:00:00'))
155+
```
156+
157+
## toDayOfMonth
158+
159+
Usage:
160+
161+
```sql
162+
toDayOfMonth(<datetime>)
163+
```
164+
165+
`toDayOfMonth` returns the day of the month from a datetime.
166+
167+
Examples:
168+
169+
```sql
170+
-- returns the number 27
171+
toDayOfMonth(toDateTime('2025-10-27 00:00:00'))
172+
```
173+
174+
## toHour
175+
176+
Usage:
177+
178+
```sql
179+
toHour(<datetime>)
180+
```
181+
182+
`toHour` returns the hour of the day from a datetime.
183+
184+
Examples:
185+
186+
```sql
187+
-- returns the number 9
188+
toHour(toDateTime('2025-10-27 09:11:13'))
189+
```
190+
191+
## toMinute
192+
193+
Usage:
194+
195+
```sql
196+
toMinute(<datetime>)
197+
```
198+
199+
`toMinute` returns the minute of the hour from a datetime.
200+
201+
Examples:
202+
203+
```sql
204+
-- returns the number 11
205+
toMinute(toDateTime('2025-10-27 09:11:13'))
206+
```
207+
208+
## toSecond
209+
210+
Usage:
211+
212+
```sql
213+
toSecond(<datetime>)
214+
```
215+
216+
`toSecond` returns the second of the minute from a datetime.
217+
218+
Examples:
219+
220+
```sql
221+
-- returns the number 13
222+
toSecond(toDateTime('2025-10-27 09:11:13'))
223+
```
224+
225+
## toStartOfYear
226+
227+
Usage:
228+
229+
```sql
230+
toStartOfYear(<datetime>)
231+
```
232+
233+
`toStartOfYear` rounds down a datetime to the nearest start of year. This can be useful
234+
for grouping data into equal-sized time ranges.
235+
236+
Examples:
237+
238+
```sql
239+
-- round a timestamp down to 2025-01-01 00:00:00
240+
toStartOfYear(toDateTime('2025-10-27 00:00:00'))
241+
```
242+
243+
## toStartOfMonth
244+
245+
Usage:
246+
247+
```sql
248+
toStartOfMonth(<datetime>)
249+
```
250+
251+
`toStartOfMonth` rounds down a datetime to the nearest start of month. This can be useful
252+
for grouping data into equal-sized time ranges.
253+
254+
Examples:
255+
256+
```sql
257+
-- round a timestamp down to 2025-10-01 00:00:00
258+
toStartOfMonth(toDateTime('2025-10-27 00:00:00'))
259+
```
260+
261+
## toStartOfDay
262+
263+
Usage:
264+
265+
```sql
266+
toStartOfDay(<datetime>)
267+
```
268+
269+
`toStartOfDay` rounds down a datetime to the nearest start of day. This can be useful
270+
for grouping data into equal-sized time ranges.
271+
272+
Examples:
273+
274+
```sql
275+
-- round a timestamp down to 2025-10-27 00:00:00
276+
toStartOfDay(toDateTime('2025-10-27 00:00:00'))
277+
```
278+
279+
## toStartOfHour
280+
281+
Usage:
282+
283+
```sql
284+
toStartOfHour(<datetime>)
285+
```
286+
287+
`toStartOfHour` rounds down a datetime to the nearest start of hour. This can be useful
288+
for grouping data into equal-sized time ranges.
289+
290+
Examples:
291+
292+
```sql
293+
-- round a timestamp down to 2025-10-27 16:00:00
294+
toStartOfHour(toDateTime('2025-10-27 16:55:25'))
295+
```
296+
297+
## toStartOfFifteenMinutes
298+
299+
Usage:
300+
301+
```sql
302+
toStartOfFifteenMinutes(<datetime>)
303+
```
304+
305+
`toStartOfFifteenMinutes` rounds down a datetime to the nearest fifteen minutes. This can be useful
306+
for grouping data into equal-sized time ranges.
307+
308+
Examples:
309+
310+
```sql
311+
-- round a timestamp down to 2025-10-27 16:45:00
312+
toStartOfFifteenMinutes(toDateTime('2025-10-27 16:55:25'))
313+
```
314+
315+
## toStartOfMinute
316+
317+
Usage:
318+
319+
```sql
320+
toStartOfMinute(<datetime>)
321+
```
322+
323+
`toStartOfMinute` rounds down a datetime to the nearest minute. This can be useful
324+
for grouping data into equal-sized time ranges.
325+
326+
Examples:
327+
328+
```sql
329+
-- round a timestamp down to 2025-10-27 16:55:00
330+
toStartOfMinute(toDateTime('2025-10-27 16:55:25'))
331+
```
332+
333+
## today
334+
335+
Usage:
336+
337+
```sql
338+
now()
339+
```
340+
341+
Returns the current date as a `Date`.

0 commit comments

Comments
 (0)