Skip to content

Commit 61c8f60

Browse files
authored
Update cookbook recipes to use TICK syntax for date filtering (#352)
Replace legacy dateadd(), today(), yesterday(), now() patterns with the new TICK syntax across 27 cookbook recipes: - timestamp IN today() → timestamp IN '$today' - timestamp IN yesterday() → timestamp IN '$yesterday' - timestamp > dateadd('d', -1, now()) → timestamp IN '$now - 1d..$now' - @lookback := dateadd(...) → @lookback := '$now - Xd..$now' Also simplifies VWAP query to use direct window function division. Adds new elapsed-time recipe in time-series patterns.
1 parent 97b7302 commit 61c8f60

27 files changed

Lines changed: 142 additions & 94 deletions

documentation/cookbook/demo-data-schema.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ The table tracks **30 currency pairs**: EURUSD, GBPUSD, USDJPY, USDCHF, AUDUSD,
6262

6363
```questdb-sql demo title="Recent core_price updates"
6464
SELECT * FROM core_price
65-
WHERE timestamp IN today()
65+
WHERE timestamp IN '$today'
6666
LIMIT -10;
6767
```
6868

@@ -116,7 +116,7 @@ SELECT timestamp, symbol,
116116
array_count(bids[1]) as bid_levels,
117117
array_count(asks[1]) as ask_levels
118118
FROM market_data
119-
WHERE timestamp IN today()
119+
WHERE timestamp IN '$today'
120120
LIMIT -5;
121121
```
122122

@@ -170,7 +170,7 @@ CREATE TABLE 'fx_trades' (
170170

171171
```questdb-sql demo title="Recent FX trades"
172172
SELECT * FROM fx_trades
173-
WHERE timestamp IN today()
173+
WHERE timestamp IN '$today'
174174
LIMIT -10;
175175
```
176176

documentation/cookbook/integrations/grafana/variable-dropdown.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ The PostgreSQL data source recognizes `__text` and `__value` as special column n
193193
**Different filter conditions:**
194194
```sql
195195
-- Filter by time range
196-
WHERE timestamp IN yesterday()
196+
WHERE timestamp IN '$yesterday'
197197

198198
-- Filter by multiple criteria
199199
WHERE symbol LIKE '%USDT' AND price > 1000

documentation/cookbook/sql/advanced/conditional-aggregates.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ SELECT
3333
sum(CASE WHEN amount <= 1.0 THEN amount END) as small_trade_volume,
3434
sum(amount) as total_volume
3535
FROM trades
36-
WHERE timestamp >= dateadd('d', -1, now())
36+
WHERE timestamp IN '$now - 1d..$now'
3737
AND symbol IN ('BTC-USDT', 'ETH-USDT')
3838
GROUP BY symbol;
3939
```

documentation/cookbook/sql/advanced/consistent-histogram-buckets.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ SELECT
2020
floor(amount / @bucket_size) * @bucket_size AS bucket,
2121
count(*) AS count
2222
FROM trades
23-
WHERE symbol = 'BTC-USDT' AND timestamp IN today()
23+
WHERE symbol = 'BTC-USDT' AND timestamp IN '$today'
2424
GROUP BY bucket
2525
ORDER BY bucket;
2626
```
@@ -53,7 +53,7 @@ DECLARE @bucket_count := 50
5353
5454
WITH raw_data AS (
5555
SELECT price, amount FROM trades
56-
WHERE symbol = 'BTC-USDT' AND timestamp IN today()
56+
WHERE symbol = 'BTC-USDT' AND timestamp IN '$today'
5757
),
5858
bucket_size AS (
5959
SELECT (max(price) - min(price)) / (@bucket_count - 1) AS bucket_size FROM raw_data
@@ -83,7 +83,7 @@ SELECT
8383
FROM trades
8484
WHERE symbol = 'BTC-USDT'
8585
AND amount > 0.000001 -- optional. Just adding here for easier visualization
86-
AND timestamp IN today()
86+
AND timestamp IN '$today'
8787
GROUP BY bucket
8888
ORDER BY bucket;
8989
```
@@ -104,7 +104,7 @@ SELECT
104104
END AS bucket,
105105
count(*) AS count
106106
FROM trades
107-
WHERE symbol = 'BTC-USDT' AND timestamp IN today()
107+
WHERE symbol = 'BTC-USDT' AND timestamp IN '$today'
108108
GROUP BY bucket;
109109
```
110110

@@ -119,7 +119,7 @@ SELECT
119119
floor(amount / @bucket_size) * @bucket_size AS bucket,
120120
count(*) AS count
121121
FROM trades
122-
WHERE symbol = 'BTC-USDT' AND timestamp IN today()
122+
WHERE symbol = 'BTC-USDT' AND timestamp IN '$today'
123123
SAMPLE BY 1h
124124
ORDER BY timestamp, bucket;
125125
```

documentation/cookbook/sql/advanced/local-min-max.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ SELECT timestamp, bid_price,
1919
min(bid_price) OVER (ORDER BY timestamp RANGE 1 second PRECEDING) AS min_price,
2020
max(bid_price) OVER (ORDER BY timestamp RANGE 1 second PRECEDING) AS max_price
2121
FROM core_price
22-
WHERE timestamp >= dateadd('m', -1, now()) AND symbol = 'EURUSD';
22+
WHERE timestamp IN '$now - 1m..$now' AND symbol = 'EURUSD';
2323
```
2424

2525
This returns the minimum and maximum bid price from the 1 second preceding each row.
@@ -35,7 +35,7 @@ SELECT p.timestamp, p.bid_price,
3535
FROM core_price p
3636
WINDOW JOIN core_price pp ON symbol
3737
RANGE BETWEEN 1 second PRECEDING AND 1 second FOLLOWING
38-
WHERE p.timestamp >= dateadd('m', -1, now()) AND p.symbol = 'EURUSD';
38+
WHERE p.timestamp IN '$now - 1m..$now' AND p.symbol = 'EURUSD';
3939
```
4040

4141
This returns the minimum and maximum bid price from 1 second before to 1 second after each row.

documentation/cookbook/sql/advanced/pivot-with-others.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ You want to pivot data so that specific symbols (like EURUSD, GBPUSD, USDJPY) be
1313
```questdb-sql demo title="Aggregated data per symbol"
1414
SELECT timestamp, symbol, SUM(bid_volume) AS total_bid
1515
FROM core_price
16-
WHERE timestamp IN today()
16+
WHERE timestamp IN '$today'
1717
SAMPLE BY 1m
1818
LIMIT 20;
1919
```
@@ -55,7 +55,7 @@ SELECT timestamp,
5555
SUM(CASE WHEN symbol NOT IN ('EURUSD', 'GBPUSD', 'USDJPY')
5656
THEN bid_volume END) AS OTHERS
5757
FROM core_price
58-
WHERE timestamp IN today()
58+
WHERE timestamp IN '$today'
5959
SAMPLE BY 1m
6060
LIMIT 5;
6161
```

documentation/cookbook/sql/advanced/rows-before-after-value-match.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ SELECT timestamp, bid_price,
2727
LEAD(bid_price, 4) OVER () AS next_4,
2828
LEAD(bid_price, 5) OVER () AS next_5
2929
FROM core_price
30-
WHERE timestamp >= dateadd('m', -1, now()) AND symbol = 'EURUSD';
30+
WHERE timestamp IN '$now - 1m..$now' AND symbol = 'EURUSD';
3131
```
3232

3333
## How it works

documentation/cookbook/sql/advanced/sankey-funnel.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ WITH PrevEvents AS (
4343
timestamp,
4444
lag(timestamp) OVER (PARTITION BY visitor_id ORDER BY timestamp) AS prev_ts
4545
FROM
46-
events WHERE timestamp > dateadd('d', -7, now())
46+
events WHERE timestamp IN '$now - 7d..$now'
4747
AND metric_name = 'page_view'
4848
), VisitorSessions AS (
4949
SELECT *,

documentation/cookbook/sql/advanced/top-n-plus-others.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ WITH totals AS (
3131
symbol,
3232
count() as total
3333
FROM trades
34-
WHERE timestamp >= dateadd('d', -1, now())
34+
WHERE timestamp IN '$now - 1d..$now'
3535
),
3636
ranked AS (
3737
SELECT
@@ -160,7 +160,7 @@ Results in three groups: top 5 individual, ranks 6-10 combined, rest combined.
160160
WITH totals AS (
161161
SELECT symbol, count() as total
162162
FROM trades
163-
WHERE timestamp >= dateadd('d', -1, now())
163+
WHERE timestamp IN '$now - 1d..$now'
164164
),
165165
ranked AS (
166166
SELECT *, rank() OVER (ORDER BY total DESC) as ranking
@@ -196,7 +196,7 @@ WITH totals AS (
196196
side,
197197
count() as total
198198
FROM trades
199-
WHERE timestamp >= dateadd('d', -1, now())
199+
WHERE timestamp IN '$now - 1d..$now'
200200
),
201201
ranked AS (
202202
SELECT

documentation/cookbook/sql/advanced/unpivot-table.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ WITH pivoted AS (
4242
CASE WHEN side = 'buy' THEN price END as buy,
4343
CASE WHEN side = 'sell' THEN price END as sell
4444
FROM trades
45-
WHERE timestamp >= dateadd('m', -5, now())
45+
WHERE timestamp IN '$now - 5m..$now'
4646
AND symbol = 'ETH-USDT'
4747
),
4848
unpivoted AS (
@@ -114,7 +114,7 @@ WITH sensor_data AS (
114114
humidity,
115115
pressure
116116
FROM sensors
117-
WHERE timestamp >= dateadd('h', -1, now())
117+
WHERE timestamp IN '$now - 1h..$now'
118118
)
119119
SELECT timestamp, sensor_id, 'temperature' as metric, temperature as value FROM sensor_data
120120
WHERE temperature IS NOT NULL

0 commit comments

Comments
 (0)