tools-and-features-hogql
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHogQL queries for PostHog
用于PostHog分析的HogQL查询
This skill helps you write HogQL queries for PostHog analytics. HogQL is PostHog's SQL dialect, a wrapper around ClickHouse SQL with simplified property access and PostHog-specific functions.
本技能可帮助你编写用于PostHog分析的HogQL查询。HogQL是PostHog的SQL方言,是ClickHouse SQL的包装器,具有简化的属性访问和PostHog专属函数。
Reference files
参考文件
- - Sql expressions - docs
references/expressions.md - - Supported aggregations - docs
references/aggregations.md - - Supported clickhouse functions - docs
references/clickhouse-functions.md - - Accessing data using sql - docs
references/data-access.md - - Sql variables - docs
references/variables.md - - Useful sql functions - docs
references/useful-functions.md - - PostHog table schemas (events, persons, groups, sessions)
references/posthog.md - - Sessions - docs
references/sessions.md
Consult the documentation for SQL syntax, available functions, and query patterns.
- - SQL表达式 - 文档
references/expressions.md - - 支持的聚合操作 - 文档
references/aggregations.md - - 支持的ClickHouse函数 - 文档
references/clickhouse-functions.md - - 使用SQL访问数据 - 文档
references/data-access.md - - SQL变量 - 文档
references/variables.md - - 实用SQL函数 - 文档
references/useful-functions.md - - PostHog表结构(events、persons、groups、sessions)
references/posthog.md - - 会话 - 文档
references/sessions.md
请查阅文档了解SQL语法、可用函数和查询模式。
Key principles
核心原则
- Property access: Use for event properties and
properties.$property_namefor person propertiesperson.properties.$property_name - Null handling: HogQL has simplified null handling compared to raw ClickHouse SQL
- Filters placeholder: Use in queries to allow UI-based filtering in PostHog dashboards
{filters} - Aggregations: Prefer ClickHouse aggregation functions like ,
count(),uniq(),avg()sum()
- 属性访问:使用访问事件属性,使用
properties.$property_name访问用户属性person.properties.$property_name - 空值处理:与原生ClickHouse SQL相比,HogQL的空值处理更简化
- 过滤器占位符:在查询中使用,以便在PostHog仪表盘中实现基于UI的过滤
{filters} - 聚合操作:优先使用ClickHouse聚合函数,如、
count()、uniq()、avg()sum()
Common patterns
常见模式
Event queries
事件查询
sql
SELECT event, count()
FROM events
WHERE {filters}
GROUP BY event
ORDER BY count() DESCsql
SELECT event, count()
FROM events
WHERE {filters}
GROUP BY event
ORDER BY count() DESCProperty breakdowns
属性细分
sql
SELECT properties.$browser AS browser, count()
FROM events
WHERE event = '$pageview' AND {filters}
GROUP BY browsersql
SELECT properties.$browser AS browser, count()
FROM events
WHERE event = '$pageview' AND {filters}
GROUP BY browserPerson properties
用户属性
sql
SELECT person.properties.email, count()
FROM events
WHERE {filters}
GROUP BY person.properties.emailsql
SELECT person.properties.email, count()
FROM events
WHERE {filters}
GROUP BY person.properties.emailFramework guidelines
框架指南
- Use properties.$name syntax for event properties, person.properties.$name for person properties
- Use bracket notation for special characters like properties['$feature/cool-flag']
- For cohorts, filter with person_id IN COHORT 'cohort-name'
- For actions, use matchesAction('action-name') in WHERE clauses
- Include {filters} placeholder in WHERE clauses to enable UI-based filtering in dashboards
- Use {variables.name} for reusable SQL variables across dashboards
- Access dashboard date range with {filters.dateRange.from} and {filters.dateRange.to}
- ALWAYS include a time range filter - shorter is faster (e.g., timestamp >= now() - INTERVAL 7 DAY)
- Prefer uniq() over count(distinct) for counting unique values - it's more efficient
- Don't scan the same table multiple times - use materialized views for reusable subsets
- Use timestamp-based pagination instead of OFFSET for large datasets
- Name queries descriptively for easier debugging in query_log
- Use dateTrunc() for time-based grouping (e.g., dateTrunc('day', timestamp))
- For funnel queries, use windowFunnel() or sequenceMatch() functions
- Test queries in the PostHog SQL editor before using them in insights or the API
- 对事件属性使用语法,对用户属性使用
properties.$name语法person.properties.$name - 对特殊字符使用括号表示法,如
properties['$feature/cool-flag'] - 针对用户分群,使用进行过滤
person_id IN COHORT 'cohort-name' - 针对操作,在WHERE子句中使用
matchesAction('action-name') - 在WHERE子句中包含占位符,以在仪表盘中启用基于UI的过滤
{filters} - 使用实现跨仪表板的可复用SQL变量
{variables.name} - 通过和
{filters.dateRange.from}访问仪表板日期范围{filters.dateRange.to} - 务必包含时间范围过滤器——范围越短查询速度越快(例如:)
timestamp >= now() - INTERVAL 7 DAY - 统计唯一值时优先使用而非
uniq()——前者效率更高count(distinct) - 不要多次扫描同一张表——对可复用子集使用物化视图
- 针对大型数据集,使用基于时间戳的分页而非OFFSET
- 为查询命名时要清晰描述,以便在query_log中调试
- 使用进行基于时间的分组(例如:
dateTrunc())dateTrunc('day', timestamp) - 针对漏斗查询,使用或
windowFunnel()函数sequenceMatch() - 在将查询用于洞察或API之前,先在PostHog SQL编辑器中测试