SELECT
name,
salary
FROM
People
WHERE
NAMEIN ( SELECTDISTINCTNAMEFROM population WHERE country = "Canada"AND city = "Toronto" )
AND salary >= (
SELECT
AVG( salary )
FROM
salaries
WHERE
gender = "Female")
with toronto_ppl as (
SELECTDISTINCTname
FROM population
WHERE country = "Canada"
AND city = "Toronto"
)
, avg_female_salary as (
SELECTAVG(salary) as avgSalary
FROM salaries
WHERE gender = "Female"
)
SELECTname
, salary
FROM People
WHEREnamein (SELECTDISTINCTFROM toronto_ppl)
AND salary >= (SELECT avgSalary FROM avg_female_salary)
现在很清楚,Where子句是在多伦多的名称中过滤。如果您注意到,CTE很有用,因为您可以将代码分解为较小的块,但它们也很有用,因为它允许您为每个CTE分配变量名称(即toronto_ppl和avg_female_salary)
同样,CTEs允许您完成更高级的技术,如创建递归表。
-
锚构件:返回CTE的基本结果的初始查询
-
递归成员:引用CTE的递归查询。这是所有与锚构件的联盟
-
停止递归构件的终止条件
with org_structure as (
SELECT id
, manager_id
FROM staff_members
WHERE manager_id IS NULL
UNION ALL
SELECT sm.id
, sm.manager_id
FROM staff_members sm
INNER JOIN org_structure os
ON os.id = sm.manager_id
-
它允许您将代码的块分解为较小的代码块
-
它适用于写入清洁代码
-
它可以防止重复,并允许您重用类似于使用Python中的函数的代码。
SELECT name
, CASEWHEN tenure < 1THEN"analyst"
WHEN tenure BETWEEN1and3THEN"associate"
WHEN tenure BETWEEN3and5THEN"senior"
WHEN tenure > 5THEN"vp"
ELSE"n/a"
ENDAS seniority
FROM employees
CREATE TEMPORARYFUNCTION get_seniority(tenure INT64) AS (
CASEWHEN tenure < 1THEN"analyst"
WHEN tenure BETWEEN1and3THEN"associate"
WHEN tenure BETWEEN3and5THEN"senior"
WHEN tenure > 5THEN"vp"
ELSE"n/a"
END
);
SELECTname
, get_seniority(tenure) as seniority
FROM employees
Initial table:
+------+---------+-------+
| id | revenue | month |
+------+---------+-------+
| 1 | 8000 | Jan |
| 2 | 9000 | Jan |
| 3 | 10000 | Feb |
| 1 | 7000 | Feb |
| 1 | 6000 | Mar |
+------+---------+-------+
Result table:
+------+-------------+-------------+-------------+-----+-----------+
| id | Jan_Revenue | Feb_Revenue | Mar_Revenue | ... | Dec_Revenue |
+------+-------------+-------------+-------------+-----+-----------+
| 1 | 8000 | 7000 | 6000 | ... | null |
| 2 | 9000 | null | null | ... | null |
| 3 | null | 10000 | null | ... | null |
+------+-------------+-------------+-------------+-----+-----------+
+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+Answer:
SELECT
a.Name as Employee
FROM
Employee as a
JOIN Employee as b on a.ManagerID = b.Id
WHERE a.Salary > b.Salary
-
按购物,利润等数量排名最高值的客户
-
排名销售数量的顶级产品
-
以最大的销售排名顶级国家
-
排名在观看的分钟数,不同观众的数量等观看的顶级视频。
SELECT Name
, GPA
, ROW_NUMBER() OVER (ORDER BY GPA desc)
, RANK() OVER (ORDER BY GPA desc)
, DENSE_RANK() OVER (ORDER BY GPA desc)
FROM student_grades
ROW_NUMBER()返回每行开始的唯一编号。当存在关系时(例如,BOB vs Carrie),ROW_NUMBER()如果未定义第二条标准,则任意分配数字。
Rank()返回从1开始的每行的唯一编号,除了有关系时,Rank()将分配相同的数字。同样,差距将遵循重复的等级。
dense_rank()类似于Rank(),除了重复等级后没有间隙。请注意,使用dense_rank(),Daniel排名第3,而不是第4位。
Lead()和LAG()发挥作用时。
# Comparing each month's sales to last month
SELECTmonth
, sales
, sales - LAG(sales, 1) OVER (ORDERBYmonth)
FROM monthly_sales
# Comparing each month's sales to the same month last year
SELECTmonth
, sales
, sales - LAG(sales, 12) OVER (ORDERBYmonth)
FROM monthly_sales
如果你知道关于row_number()和lag()/lead(),这可能对您来说可能不会惊喜。但如果你没有,这可能是最有用的窗口功能之一,特别是当您想要可视化增长!
使用具有SUM()的窗口函数,我们可以计算运行总数。请参阅下面的示例:
+---------+------------------+------------------+
| Id(INT) | RecordDate(DATE) | Temperature(INT) |
+---------+------------------+------------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+---------+------------------+------------------+Answer:
SELECT
a.Id
FROM
Weather a,
Weather b
WHERE
a.Temperature > b.Temperature
ANDDATEDIFF(a.RecordDate, b.RecordDate) = 1
往期推荐
Cursor Pro Ai编程工具 永久激活 无限续杯白嫖最高模型
开启Cloudflare邮箱接管 详细流程
SpringBoot3+国产FolkMQ!5行代码搞定消息中间件
IntelliJ IDEA 2025.2 最新激活教程,一键激活2099
如何优雅的实现在线人数统计功能?
Http 接口对接太繁琐?试试 UniHttp 框架吧!简简单单~

