---
title: "Forecasting Using ClickHouse Machine Learning Functions"
description: "ClickHouse's built-in stochasticLinearRegression and evalMLMethod functions can fit and apply time-series forecasting models entirely in SQL, with no Python or R. Using monthly flight passenger data, a log-transformed target is regressed on a linear trend plus 12 month dummy variables, with no intercept to avoid the dummy variable trap."
url: "https://swe.quest/articles/forecasting-using-clickhouse-machine-learning-functions-69c536e4a757b31c"
canonical: "https://clickhouse.com/blog/forecasting-using-clickhouse"
source: "ClickHouse"
published: "2023-10-26"
difficulty: "medium"
kind: "How-to"
reading_minutes: 9
tags: ["analytics","machine-learning","sql"]
---

# Forecasting Using ClickHouse Machine Learning Functions

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2023-10-26, 9 min read, How-to. Original: [https://clickhouse.com/blog/forecasting-using-clickhouse](https://clickhouse.com/blog/forecasting-using-clickhouse)

## Summary

ClickHouse's built-in stochasticLinearRegression and evalMLMethod functions can fit and apply time-series forecasting models entirely in SQL, with no Python or R. Using monthly flight passenger data, a log-transformed target is regressed on a linear trend plus 12 month dummy variables, with no intercept to avoid the dummy variable trap. One model per airline-airport pair is trained in a single GROUP BY query, giving 35 models. Test-set MAE and RMSE are computed in SQL, and a future-dates view extends the forecasts three years out. Wrapping training in a materialized view would let the models retrain continuously as new data arrives.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)
- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [SQL](https://swe.quest/tags/sql.md) (Languages)

## Prerequisites

- SQL aggregation ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- linear regression basics
- time-series seasonality

## Keywords

ClickHouse, stochasticLinearRegression, evalMLMethod, SGD

## Related posts

- [Linear Regression Using ClickHouse Machine Learning Functions](https://swe.quest/articles/linear-regression-using-clickhouse-machine-learning-functions-74857eccf0056a32.md): ClickHouse · 2023-12-13 · Medium · 7 min read · Machine learning, Analytics & OLAP, SQL, Maps & geospatial
  Delivery times can be predicted with a linear regression trained entirely inside ClickHouse using stochasticLinearRegressionState and evalMLMethod, with no Python or Spark involved.
- [ClickHouse forecasting and anomaly detection with time series foundation models](https://swe.quest/articles/clickhouse-forecasting-and-anomaly-detection-with-time-series-foundation-models-71a24c218275d2ec.md): ClickHouse · 2026-11-17 · Medium · 11 min read · Analytics & OLAP, Machine learning, Inference & serving, SQL
  TimesFM 3.0, a pretrained time-series foundation model, can be called from ClickHouse SQL via executable Python UDFs that forward arrays to an HTTP model server, giving zero-shot forecasts and anomaly scores without per-series training.
- [Probabilistic Time Series Forecasting with 🤗 Transformers](https://swe.quest/articles/probabilistic-time-series-forecasting-with-transformers-d4a4a66f913813a3.md): Hugging Face · 2022-12-01 · Hard · 19 min read · Machine learning, Model training, Python
  An encoder-decoder Transformer can be trained as a global probabilistic forecaster that learns across many related time series and outputs a distribution (Student-t by default) rather than point estimates, with forecasts sampled autoregressively.
- [TimesFM-3: A zero-shot foundation model for multivariate forecasting](https://swe.quest/articles/timesfm-3-a-zero-shot-foundation-model-for-multivariate-forecasting-c2421e383bcd85af.md): Google Research · 2026-08-31 · Hard · 6 min read · Machine learning, Model training, Inference & serving
  TimesFM-3 is a 330M-parameter decoder-only transformer pre-trained on over 1 trillion real and synthetic time points that does zero-shot multivariate forecasting, supporting multiple targets, past-only covariates and known-future covariates.
- [Modeling Machine Learning Data in OLAP databases](https://swe.quest/articles/modeling-machine-learning-data-in-olap-databases-0fad2c579add8f45.md): ClickHouse · 2024-08-08 · Medium · 26 min read · ML platforms, Analytics & OLAP, SQL, Data pipelines
  ClickHouse can act as the transformation engine and offline store of an ML feature store, with features modeled as timestamped tables and assembled into point-in-time correct training sets.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
