当前位置:网站首页>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. !
边栏推荐
- 雷达成像 Matlab 仿真 4 —— 距离分辨率分析
- Perl入门学习(十一)文件操作
- Example of frameset usage
- PyTorch 学习笔记 3 —— DATASETS & DATALOADERS & TRANSFORMS
- Electric fast burst (EFT) design - EMC series hardware design notes 4
- 【YOLOv5】环境搭建:Win11 + mx450
- ASP. Net read database bound to treeview recursive mode
- USB network native driver for esxi updated to support esxi7.0 Update 2
- Measure computer battery capacity
- How to calibrate dsx2-8000? Calibration process?
猜你喜欢

MFC 使用控制台打印程序信息

ICC2(四)Routing and Postroute Optimization

Synopsys Multivoltage Flow

Trouble encountered in cable testing -- a case study of a manufacturer?

VAN(DWConv+DWDilationConv+PWConv)

Graduation thesis | how to write literature review

Bag of tricks training convolution network skills

Word自动目录字体修改和行间距的问题

set_ case_ analysis

Beginners choose sensors
随机推荐
根据IP地址和子网掩码求主机所在的网络地址和广播地址
低功耗设计-Power Switch
Learning notes of hardware circuit design 1 -- temperature rise design
Example of frameset usage
Design and analysis of contactor coil control circuit
Overall understanding of PLC
In asp Usage of cookies in. Net
Pytorch learning note 4 - automatic calculation of gradient descent autograd
t-SNE降维可视化
Word自动目录字体修改和行间距的问题
Pytorch learning notes
VB OCX applied to Web
Redhawk Dynamic Analysis
ASP. Net read database bound to treeview recursive mode
How to view the transfer function of the module directly built by Simulink
Esxi on ARM v1.2 (updated in November 2020)
Distinguishing PCB quality by color is a joke in itself
ICC2分析时序的神器 analyze_design_violations
Web scrolling subtitles (marquee example)
多个ics日历合并成单个ics日历
