RuleGo RuleGo
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • StreamSQL
  • AI Agent Framework
  • TPCLAW Agent Platform (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • StreamSQL
  • AI Agent Framework
  • TPCLAW Agent Platform (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文

广告采用随机轮播方式显示 ❤️成为赞助商
  • Quick Start

  • Rule Chain

  • Standard Components

  • Extension Components

  • Custom Components

  • Components marketplace

  • Visualization

  • AOP

  • Trigger

  • Advanced Topic

  • Agent Framework

  • RuleGo-Server

  • FAQ

  • Endpoint Module

  • Support

  • StreamSQL

    • Overview
    • Quick Start
    • Core Concepts
    • SQL Reference
    • API Reference
    • RuleGo Integration
    • Schema Validation
    • Advanced Examples
    • Pattern Matching (CEP)
    • functions

      • Aggregate Functions
      • Analytical Functions
        • Function List
          • lag — Previous value
          • latest — Latest non-null value
          • had_changed — Whether a value changed
          • changed_col — Changed column value (single-column scalar)
          • changed_cols — Multiple changed column values (dynamic columns)
          • accsum / accmax / accmin / acccount / acc_avg — Lifecycle accumulation
        • 📚 Related Documentation
      • Window Functions
      • Math Functions
      • String Functions
      • Conversion Functions
      • DateTime Functions
      • JSON Functions
      • Hash Functions
      • Array Functions
      • Type Check Functions
      • Conditional Functions
      • Multi-row Functions
      • Expression Functions
      • Custom Functions
    • case-studies

目录

Analytical Functions

# StreamSQL Analytical Functions

Analytical functions perform cross-event stateful computations on a continuous event stream — e.g. "previous value", "has it changed", "running sum". Each event is evaluated immediately upon arrival, with state retained across events. They are usable in SELECT and WHERE, and are typically used for change detection (CDC).

An optional OVER clause controls partitioning and conditional state:

func(args) OVER ( [PARTITION BY col[, col...]] [WHEN condition] )
1
  • PARTITION BY: maintains independent state per partition (e.g. "previous value per device").
  • WHEN condition: only events satisfying the condition update state; others reuse the last result.
  • ORDER BY / ROWS BETWEEN are not supported (streamsql's analytic functions are per-event state machines, not Flink-style window frames).

Analytic functions cannot appear in HAVING; for threshold / sustained detection use windowed aggregation + HAVING.

# Function List

# lag — Previous value

Syntax: lag(field [, offset [, default [, ignoreNull]]])
Description: Returns the value from the offset-th event before the current row (default offset=1; returns default when there are not enough rows).
Example:

SELECT temperature, lag(temperature) AS prev FROM stream
1

# latest — Latest non-null value

Syntax: latest(field [, default])
Description: Returns the latest non-null value of the field (nil does not update state).
Example:

SELECT latest(temperature) AS lt FROM stream
1

# had_changed — Whether a value changed

Syntax: had_changed(ignoreNull, field[, field...])
Description: Returns a boolean: whether any value changed vs. the previous row (the first row counts as changed). Supports multiple fields; "*" checks every column of the row.
Example:

SELECT ts FROM stream WHERE had_changed(true, status) == true
1

# changed_col — Changed column value (single-column scalar)

Syntax: changed_col(ignoreNull, field)
Description: Returns the new value when changed, nil when unchanged (omitted from projection).
Example:

SELECT changed_col(true, temperature) AS chg FROM stream
1

# changed_cols — Multiple changed column values (dynamic columns)

Syntax: changed_cols(prefix, ignoreNull, field[, field...])
Description: Returns {prefix+column: new value} containing only changed columns; "*" checks all columns of the row. SELECT only.
Example:

SELECT changed_cols("c_", true, temperature, humidity) FROM stream
1

# acc_sum / acc_max / acc_min / acc_count / acc_avg — Lifecycle accumulation

Syntax: acc_sum(field [, startExpr [, resetExpr]]) (likewise for the other acc_*)
Description: Lifecycle accumulation across events (sum / max-min / count / avg); optional startExpr / resetExpr for conditional start and reset.
Example:

SELECT acc_sum(power) AS total_power FROM stream
1

# 📚 Related Documentation

  • Aggregate Functions - Learn detailed usage of aggregate functions
  • Window Functions - Learn detailed usage of window functions
  • SQL Reference - View complete SQL syntax reference
Edit this page on GitHub (opens new window)
Last Updated: 2026/07/11, 12:44:39
Aggregate Functions
Window Functions

← Aggregate Functions Window Functions→

Theme by Vdoing | Copyright © 2023-2026 RuleGo Team | Apache 2.0 License

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式