当前位置:网站首页>Common table expression CTE in Clickhouse
Common table expression CTE in Clickhouse
2022-07-28 06:29:00 【Strange patron saint】

What is a common table expression (CTE) ? In this paper , Learn how to ClickHouse Use in the database CTE, And track use cases through examples
Use when CTE Very convenient :
- When a request can get data , And its size is suitable for memory space
- The result of this query needs to be used more than once
- Create recursive queries
The additional benefit is to improve SQL Readability of queries .
CTE What is the difference between temporary tables and nested queries ?
- If the subquery is relevant , Then its call will repeat for each line in the selection , This significantly increases the cost of executing this query .
- Filling a temporary table with a large amount of data will cause load on the disk .
- Due to the particularity of storing temporary tables , Using temporary tables to execute queries will increase execution time .
grammar
ClickHouse Support both WITH< expression > AS < identifier > , Also support WITH< identifier > AS < Subquery expression > grammar .
- Use WITH initialization CTE.
- Provide a name for the query .
- Next is AS.
- Define query
- If more than one is needed CTE, Please separate them with commas .
WITH locations AS
(
SELECT location
FROM table
WHERE date > (today() - 10)
)
SELECT *
FROM locations
- Use WITH initialization CTE.
- Define an expression
- Next is AS.
- Give the expression a name .
- If more than one is needed CTE, Please separate them with commas .
WITH ('USA', 'BRA') AS locations
SELECT 'ARG' IN (locations)
Example
Build table :
CREATE TABLE SpareParts
(
`id` UInt32,
`partName` String,
`partOrigin` String,
`storeID` UInt32
)
ENGINE = MergeTree()
ORDER BY id
Insert :
INSERT INTO SpareParts VALUES (1, 'headlight', 'USA', 1)
INSERT INTO SpareParts VALUES (2, 'hood', 'JPN', 1)
INSERT INTO SpareParts VALUES (3, 'bumper', 'USA', 1)
INSERT INTO SpareParts VALUES (4, 'radiator', 'BRA', 3)
INSERT INTO SpareParts VALUES (5, 'wheel', 'BRA', 2)
INSERT INTO SpareParts VALUES (6, 'stabilizer', 'ARG', 3)
INSERT INTO SpareParts VALUES (7, 'absorber', 'TUR', 2)
INSERT INTO SpareParts VALUES (8, 'cable', 'MEX', 1)
INSERT INTO SpareParts VALUES (9, 'spring', 'MEX', 3)
INSERT INTO SpareParts VALUES (10, 'door', 'USA', 2)
choice :
WITH
originsByStore AS
(
SELECT
storeID,
groupArray(partOrigin) AS origins
FROM SpareParts
GROUP BY storeID
),
partsByStore AS
(
SELECT
storeID,
groupArray(partName) AS partNames
FROM SpareParts
GROUP BY storeID
),
has(origins, 'USA') = 1 AS isUSA
SELECT
storeID,
origins,
partNames,
isUSA
FROM originsByStore AS t1
LEFT JOIN
(
SELECT
storeID,
partNames
FROM partsByStore
) AS t2 USING (storeID)
result :
Blogger's personal blog website :https://www.beierblog.com
The original author of this article : Whimsical school 、 A programmer who works hard to share .
The platform for the first article : WeChat official account 【 Programming talent 】

Originality is not easy. ! If you think the article is good , Let's pay attention to the official account , Conduct give the thumbs-up ( Looking at )、 forward Three companies walk up ! Thank you. !
边栏推荐
- t-SNE降维可视化
- Led selection - hardware learning notes 3
- Machine learning note 5 - logistic regression
- Cautious speculation about fusion on Apple silicon
- IMS-FACNN(Improved Multi-Scale Convolution Neural Network integrated with a Feature Attention Mecha
- 一、ffmpeg录制音频为pcm文件
- Varistor design parameters and classic circuit recording hardware learning notes 5
- Surge impact immunity experiment (surge) -emc series Hardware Design Notes 6
- Low power design -power switch
- PyTorch 学习笔记 1 —— Quick Start
猜你喜欢
随机推荐
RS232 RS485 RS422 communication learning and notes
ASP. Net read database bound to treeview recursive mode
EMC experiment practical case ESD electrostatic experiment
解决内存占用比应用进程占用高的问题
How can fluke dsx2-5000 and dsx2-8000 modules find the calibration expiration date?
[yolov5] environment construction: win11 + mx450
Beginners choose sensors
Matlab 信号处理
Design and analysis of contactor coil control circuit
qt设置加载界面的几种方法
ICC2(三)Clock Tree Synthesis
Web scrolling subtitles (marquee example)
Monitor the CPU temperature of raspberry pie 4B installed with esxi on ARM
Redhawk Dynamic Analysis
Cronbach’s α?KMO系数?因子载荷?史上最易懂的问卷信效度分析教程!!!(SPSS和AMOS)
mixup_ratio
mysql join技巧
How to use the bit error meter?
Electric fast burst (EFT) design - EMC series hardware design notes 4
Fluke dtx-sfm2 single mode module of a company in Hangzhou - repair case










