trino
TD Trino SQL
TD Time Functions
td_interval (Recommended for relative time)
where td_interval(time, '-1d', 'JST') -- Yesterday (JST)
where td_interval(time, '-1d') -- Yesterday (UTC)
where td_interval(time, '-1w', 'JST') -- Previous week
where td_interval(time, '-1M', 'JST') -- Previous month
where td_interval(time, '-1d/-1d', 'JST') -- 2 days ago
where td_interval(time, '-1M/-2M', 'JST') -- 3 months ago
Timezone is optional (defaults to UTC).
Note: Cannot use td_scheduled_time() as first arg. Include td_scheduled_time() elsewhere in query to establish reference date.
td_time_range (Explicit dates)
where td_time_range(time, '2024-01-01', '2024-01-31')
where td_time_range(time, td_time_add(td_scheduled_time(), '-7d'), td_scheduled_time())
td_time_string (Display formatting)
td_time_string(time, 'd!') -- 2024-01-15 (UTC)
td_time_string(time, 'd!', 'JST') -- 2024-01-15 (JST)
td_time_string(time, 's!', 'JST') -- 2024-01-15 10:30:45
td_time_string(time, 'M!') -- 2024-01
td_time_string(time, 'h!') -- 2024-01-15 10
Format codes: y!=year, q!=quarter, M!=month, w!=week, d!=day, h!=hour, m!=minute, s!=second. Without the exclamation mark, includes timezone offset. Timezone is optional (defaults to UTC).
Use for display only, never for filtering:
- Good:
select td_time_string(time, 'd!', 'JST') as date - Bad:
where td_time_string(time, 'd!', 'JST') = '2024-01-01'
td_time_format (Legacy)
td_time_format(time, 'yyyy-MM-dd HH:mm:ss', 'JST')
td_sessionize
select td_sessionize(time, 1800, user_id) as session_id -- 30min timeout
from events
Table Format
select * from database_name.table_name
where td_interval(time, '-1d', 'JST')
Performance
Always include time filters for partition pruning:
-- Good: Partition pruning
where td_time_range(time, '2024-01-01', '2024-01-02')
-- Bad: Full table scan
where event_type = 'click' -- Missing time filter!
Use approx_distinct() and approx_percentile() for large datasets.
Common Errors
| Error | Fix |
|---|---|
| Query exceeded memory limit | Add time filters, use approx_ functions |
| Partition not found | Verify time range syntax |
Resources
More from treasure-data/td-skills
pytd
Expert assistance for using pytd (Python SDK) to query and import data with Treasure Data. Use this skill when users need help with Python-based data analysis, querying Presto/Hive, importing pandas DataFrames, bulk data uploads, or integrating TD with Python analytical workflows.
20workflow
Manages TD workflows using `tdx wf` commands. Covers project sync (pull/push/clone), running workflows, monitoring sessions/attempts, task timeline visualization, retry/kill operations, and secrets management. Use when users need to manage, monitor, or debug Treasure Workflow projects via tdx CLI.
3journey
Load when the client wants to create, edit, or manage a CDP customer journey. Use for building journey YAML with segments, activations, and stage steps, modifying journey stages or flow logic (decision points, condition waits, A/B tests), or pushing journey changes to Treasure Data. Also load when the client wants to analyze journey performance, query journey tables, create journey dashboards, or generate journey action reports.
2parent-segment-analysis
Query and analyze CDP parent segment database data. Use `tdx ps desc -o` to get output database schema, then query customers and behavior tables. Use when exploring parent segment data, building reports, or analyzing customer attributes and behaviors.
2connector-config
Writes connector_config for segment/journey activations using `tdx connection schema <type>` to discover available fields. Use when configuring activations - always run schema command first to see connector-specific fields.
2agent
Build LLM agents using `tdx agent pull/push` with YAML/Markdown config. Covers agent.yml structure, tools (knowledge_base, agent, web_search, image_gen), @ref syntax, and knowledge bases. Use for TD AI agent development workflow.
2