Skip to Content
高级Snowflake
book

高级Snowflake

by Muhammad Fasih Ullah
September 2025
Beginner to intermediate
210 pages
3h 1m
Chinese
O'Reilly Media, Inc.
Content preview from 高级Snowflake

第 3 章 雪花 SQL Snowflake SQL

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在本章中,我将介绍一些更高级的 Snowflake SQL 函数。首先,让我们来看看 Snowflake 的执行顺序,然后转到不同的 Windows 函数,看看在哪里可以使用它们。之后,您将了解 Snowflake 的地理空间函数。

执行顺序

Snowflake 遵循非常合理的 SQL 语句执行顺序。不过,随着QUALIFY 子句的引入,情况变得有点有趣。很多使用 Windows 函数和QUALIFY 的 Snowflake 用户对什么将在何时执行以及这将如何影响查询结果感到困惑。 本质上,QUALIFY 子句与WHERE 子句非常相似,因为它过滤数据。但是,在执行顺序上,QUALIFY 子句有所不同,因此产生了混淆。Snowflake 按以下顺序执行 SQL 子句:

  1. FROM

  2. WHERE

  3. GROUP BY

  4. HAVING

  5. WINDOW

  6. QUALIFY

  7. DISTINCT

  8. ORDER BY

  9. LIMIT

让我们来看看执行以下查询时会发生什么:

SELECT *
FROM legends
WHERE first_name = 'Cristiano'
   AND last_name = 'Ronaldo';

这是一个简单的查询,使用非常简单的WHERE 子句提取详细信息。在这种情况下,Snowflake 将首先执行FROM 子句,该子句告诉 Snowflake 从哪个表读取数据。然后是WHERE 子句,它告诉 Snowflake 要过滤哪些数据,或者换句话说,要读取哪些微分区,跳过哪些微分区。

接下来是GROUP BYHAVING 子句。GROUP BYWHERE 子句之后运行过滤后的数据,用于聚合数据。 HAVING 子句有助于过滤GROUP BY 子句的结果。假设我们有一个名为Employees 的表,其中记录了数千名员工、他们所在的部门以及他们的工资,工资从 100 美元到 5,000 美元不等。

让我们看看表 3-1 中的这两个查询。

表 3-1. 执行顺序 (WHEREHAVING)
查询 1 查询 2
SELECT DEPARTMENT, 
	SUM(SALARY) AS 
	TOTAL_SALARY
FROM EMPLOYEES
GROUP BY DEPARTMENT 
HAVING TOTAL_SALARY < 1000
SELECT DEPARTMENT, 
	SUM(SALARY) AS
	TOTAL_SALARY
FROM EMPLOYEES
WHERE SALARY < 1000 
GROUP BY DEPARTMENT
该查询读取表 employees,按部门分组,求该部门所有员工的工资总和,最后筛选出这些部门所有员工的工资总额小于 1000 的部门。该查询是对请求 "给我工资总额小于 1000 的部门 "的回答。 该查询读取雇员表,筛选出工资小于 1000 的雇员,按部门分组,然后求出该部门所有剩余工资的总和。该查询是对请求 "给我每个部门所有工资总额低于 1000 的员工的工资总额 "的回答。在这种情况下 TOTAL_SALARY列总是高于 1000,因为它过滤了工资较低的员工。

表 3-1中的两个查询有一个非常细微的差别,结果却完全不同。现在我们来看看第三个变体,它带有WHEREHAVING ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

用数据进行沟通

用数据进行沟通

Carl Allchin
可解释人工智能实践指南

可解释人工智能实践指南

Michael Munn, David Pitman

Publisher Resources

ISBN: 9798341671003