From 09b98c0195b054caf2f8c270c2d0ccde239c57d9 Mon Sep 17 00:00:00 2001 From: meelahme Date: Thu, 31 Jul 2025 17:39:43 -0700 Subject: [PATCH 1/6] docs: add dynamic date range filtering examples to WHERE clause --- content/shared/sql-reference/where.md | 58 +++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/content/shared/sql-reference/where.md b/content/shared/sql-reference/where.md index 79ac64cee8..bebdd7efc9 100644 --- a/content/shared/sql-reference/where.md +++ b/content/shared/sql-reference/where.md @@ -96,6 +96,64 @@ less than or equal to `08-19-2019T13:00:00Z`. {{% /expand %}} {{< /expand-wrapper >}} +### Filter data by dynamic date ranges + +Use date and time functions to filter data by relative time periods that automatically update. + +#### Get data from yesterday + +```sql +SELECT * +FROM h2o_feet +WHERE "location" = 'santa_monica' + AND time >= DATE_TRUNC('day', NOW() - INTERVAL '1 day') + AND time < DATE_TRUNC('day', NOW()) +``` + +{{< expand-wrapper >}} +{{% expand "View query explanation" %}} + +This query filters data to include only records from the previous calendar day: + +- `NOW() - INTERVAL '1 day'` calculates yesterday's timestamp +- `DATE_TRUNC('day', ...)` truncates to the start of that day (00:00:00) +- The range spans from yesterday at 00:00:00 to today at 00:00:00 + +{{% /expand %}} +{{< /expand-wrapper >}} + +#### Get data from the last 24 hour + +```sql +SELECT * +FROM h2o_feet +WHERE time >= NOW() - INTERVAL '1 day' +``` + +{{< expand-wrapper >}} +{{% expand "View query explanation" %}} + +This query returns data from exactly 24 hours before the current time. Unlike the "yesterday" example, this creates a rolling 24-hour window that moves with the current time. + +{{% /expand %}} +{{< /expand-wrapper >}} + +#### Get data from the current week + +```sql +SELECT * +FROM h2o_feet +WHERE time >= DATE_TRUNC('week', NOW()) +``` + +{{< expand-wrapper >}} +{{% expand "View query explanation" %}} + +This query returns all data from the start of the current week (Monday at 00:00:00) to the current time. The DATE_TRUNC('week', NOW()) function truncates the current timestamp to the beginning of the week. + +{{% /expand %}} +{{< /expand-wrapper >}} + ### Filter data using the OR operator ```sql From 5e9e0cc97bee9f63490e4b3b460565e379cfce44 Mon Sep 17 00:00:00 2001 From: Jameelah Mercer <36314199+MeelahMe@users.noreply.github.com> Date: Thu, 31 Jul 2025 17:42:01 -0700 Subject: [PATCH 2/6] Update content/shared/sql-reference/where.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- content/shared/sql-reference/where.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/shared/sql-reference/where.md b/content/shared/sql-reference/where.md index bebdd7efc9..f1163eae0e 100644 --- a/content/shared/sql-reference/where.md +++ b/content/shared/sql-reference/where.md @@ -122,7 +122,7 @@ This query filters data to include only records from the previous calendar day: {{% /expand %}} {{< /expand-wrapper >}} -#### Get data from the last 24 hour +#### Get data from the last 24 hours ```sql SELECT * From 77aed8468ffee13ec7f58ca1303a5a0eb8782a31 Mon Sep 17 00:00:00 2001 From: meelahme Date: Thu, 31 Jul 2025 22:26:53 -0700 Subject: [PATCH 3/6] docs: tested and updated examples --- content/shared/sql-reference/where.md | 28 +++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/content/shared/sql-reference/where.md b/content/shared/sql-reference/where.md index bebdd7efc9..40bbfb81e9 100644 --- a/content/shared/sql-reference/where.md +++ b/content/shared/sql-reference/where.md @@ -111,7 +111,7 @@ WHERE "location" = 'santa_monica' ``` {{< expand-wrapper >}} -{{% expand "View query explanation" %}} +{{% expand "View example results" %}} This query filters data to include only records from the previous calendar day: @@ -119,10 +119,19 @@ This query filters data to include only records from the previous calendar day: - `DATE_TRUNC('day', ...)` truncates to the start of that day (00:00:00) - The range spans from yesterday at 00:00:00 to today at 00:00:00 +| level description | location | time | water_level | +| :---------------- | :----------- | :----------------------- | :---------- | +| below 3 feet | santa_monica | 2019-08-18T12:00:00.000Z | 2.533 | +| below 3 feet | santa_monica | 2019-08-18T12:06:00.000Z | 2.543 | +| below 3 feet | santa_monica | 2019-08-18T12:12:00.000Z | 2.385 | +| below 3 feet | santa_monica | 2019-08-18T12:18:00.000Z | 2.362 | +| below 3 feet | santa_monica | 2019-08-18T12:24:00.000Z | 2.405 | +| below 3 feet | santa_monica | 2019-08-18T12:30:00.000Z | 2.398 | + {{% /expand %}} {{< /expand-wrapper >}} -#### Get data from the last 24 hour +#### Get data from the last 24 hours ```sql SELECT * @@ -131,10 +140,19 @@ WHERE time >= NOW() - INTERVAL '1 day' ``` {{< expand-wrapper >}} -{{% expand "View query explanation" %}} +{{% expand "View example results" %}} This query returns data from exactly 24 hours before the current time. Unlike the "yesterday" example, this creates a rolling 24-hour window that moves with the current time. +| level description | location | time | water_level | +| :---------------- | :----------- | :----------------------- | :---------- | +| below 3 feet | santa_monica | 2019-08-18T18:00:00.000Z | 2.120 | +| below 3 feet | santa_monica | 2019-08-18T18:06:00.000Z | 2.028 | +| below 3 feet | santa_monica | 2019-08-18T18:12:00.000Z | 1.982 | +| below 3 feet | santa_monica | 2019-08-19T06:00:00.000Z | 1.825 | +| below 3 feet | santa_monica | 2019-08-19T06:06:00.000Z | 1.753 | +| below 3 feet | santa_monica | 2019-08-19T06:12:00.000Z | 1.691 | + {{% /expand %}} {{< /expand-wrapper >}} @@ -147,10 +165,12 @@ WHERE time >= DATE_TRUNC('week', NOW()) ``` {{< expand-wrapper >}} -{{% expand "View query explanation" %}} +{{% expand "View example results" %}} This query returns all data from the start of the current week (Monday at 00:00:00) to the current time. The DATE_TRUNC('week', NOW()) function truncates the current timestamp to the beginning of the week. +level description location timew ater_levelbelow 3 feetsanta_monica2019-08-12T00:00:00.000Z2.064below 3 feetsanta_monica2019-08-14T09:30:00.000Z2.116below 3 feetsanta_monica2019-08-16T15:45:00.000Z1.952below 3 feetsanta_monica2019-08-18T12:00:00.000Z2.533below 3 feetsanta_monica2019-08-18T18:00:00.000Z2.385below 3 feetsanta_monica2019-08-19T10:30:00.000Z1.691 + {{% /expand %}} {{< /expand-wrapper >}} From 77c43889e032ac13041f0d444d29bbb9ab276ab2 Mon Sep 17 00:00:00 2001 From: meelahme Date: Thu, 31 Jul 2025 22:28:33 -0700 Subject: [PATCH 4/6] docs: updated current week example --- content/shared/sql-reference/where.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/content/shared/sql-reference/where.md b/content/shared/sql-reference/where.md index 40bbfb81e9..add7b35ded 100644 --- a/content/shared/sql-reference/where.md +++ b/content/shared/sql-reference/where.md @@ -169,7 +169,14 @@ WHERE time >= DATE_TRUNC('week', NOW()) This query returns all data from the start of the current week (Monday at 00:00:00) to the current time. The DATE_TRUNC('week', NOW()) function truncates the current timestamp to the beginning of the week. -level description location timew ater_levelbelow 3 feetsanta_monica2019-08-12T00:00:00.000Z2.064below 3 feetsanta_monica2019-08-14T09:30:00.000Z2.116below 3 feetsanta_monica2019-08-16T15:45:00.000Z1.952below 3 feetsanta_monica2019-08-18T12:00:00.000Z2.533below 3 feetsanta_monica2019-08-18T18:00:00.000Z2.385below 3 feetsanta_monica2019-08-19T10:30:00.000Z1.691 +| level description | location | time | water_level | +| :---------------- | :----------- | :----------------------- | :---------- | +| below 3 feet | santa_monica | 2019-08-12T00:00:00.000Z | 2.064 | +| below 3 feet | santa_monica | 2019-08-14T09:30:00.000Z | 2.116 | +| below 3 feet | santa_monica | 2019-08-16T15:45:00.000Z | 1.952 | +| below 3 feet | santa_monica | 2019-08-18T12:00:00.000Z | 2.533 | +| below 3 feet | santa_monica | 2019-08-18T18:00:00.000Z | 2.385 | +| below 3 feet | santa_monica | 2019-08-19T10:30:00.000Z | 1.691 | {{% /expand %}} {{< /expand-wrapper >}} From c0aff8f47580c61e7f2a9390c4b4ec3361e35eb1 Mon Sep 17 00:00:00 2001 From: Jameelah Mercer <36314199+MeelahMe@users.noreply.github.com> Date: Thu, 31 Jul 2025 22:31:52 -0700 Subject: [PATCH 5/6] Update content/shared/sql-reference/where.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- content/shared/sql-reference/where.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/shared/sql-reference/where.md b/content/shared/sql-reference/where.md index add7b35ded..f7dc25246d 100644 --- a/content/shared/sql-reference/where.md +++ b/content/shared/sql-reference/where.md @@ -161,7 +161,7 @@ This query returns data from exactly 24 hours before the current time. Unlike th ```sql SELECT * FROM h2o_feet -WHERE time >= DATE_TRUNC('week', NOW()) +WHERE time >= DATE_TRUNC('week', NOW()) AND location = 'santa_monica' ``` {{< expand-wrapper >}} From 45bdbb409cbca3fe1aba20e0927d166d9f3d5d50 Mon Sep 17 00:00:00 2001 From: Jameelah Mercer <36314199+MeelahMe@users.noreply.github.com> Date: Thu, 31 Jul 2025 22:31:58 -0700 Subject: [PATCH 6/6] Update content/shared/sql-reference/where.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- content/shared/sql-reference/where.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/shared/sql-reference/where.md b/content/shared/sql-reference/where.md index f7dc25246d..b28e3aa978 100644 --- a/content/shared/sql-reference/where.md +++ b/content/shared/sql-reference/where.md @@ -136,7 +136,7 @@ This query filters data to include only records from the previous calendar day: ```sql SELECT * FROM h2o_feet -WHERE time >= NOW() - INTERVAL '1 day' +WHERE time >= NOW() - INTERVAL '1 day' AND location = 'santa_monica' ``` {{< expand-wrapper >}}