3

Is there any option how to create a computed column in the CREATE TABLE statement? Something like generated column in PostgreSQL

CREATE TABLE people (
    ...,
    height_cm numeric,
    height_in numeric GENERATED ALWAYS AS (height_cm / 2.54) STORED
);

or computed column in T-SQL

CREATE TABLE dbo.Products
(
  ProductID int IDENTITY (1,1) NOT NULL
  , QtyAvailable smallint
  , UnitPrice money
  , InventoryValue AS QtyAvailable * UnitPrice
);

If there is no similar option, what could be the alternative please? My intention is to use these generated column in the materialized views without needs to compute them in MV create scripts.

y0j0
  • 3,369
  • 5
  • 31
  • 52

1 Answers1

4
  1. MATERIALIZED columns for storing computed values https://clickhouse.com/docs/en/sql-reference/statements/create/table/#materialized
  2. ALIAS columns to compute column values on the fly https://clickhouse.com/docs/en/sql-reference/statements/create/table/#alias
Andrei Koch
  • 898
  • 1
  • 7
  • 23
  • thanks a lot, I went through these but was not able to find proper expression because of lack of examples. Finally it works like this `ts DateTime MATERIALIZED toDateTime(ts_ms)` – y0j0 Jan 31 '22 at 16:13