`

How to code a subquery factoring clause

 
阅读更多

Subquery factoring allows you to name a block of code that contains a SELECT statement.

 

  • A subquery factoring clause can be thought of as a named subquery block. This name can then be used multiple times in the query.
  • To define a subquery factoring block, your code the WITH keyword followed by the definition of the subquery.
  • To code multiple subquery factoring clauses, separate them with commas. Then, each clause can refer to itself and any previously defined subquery factoring clauses in the same WITH clauses.
  • You can use subquery factoring clauses with SELECT, INSERT, UPDATE, and DELETE statements. However, you're most likely to use them with SELECT statements.

The syntax of a subquery factoring clause


 

Two query names and a query that uses them

with summary as
(
     select vendor_state, vendor_name, sum(invoice_total) as sum_of_invoices
     from invoices i, vendors v
     where i.vendor_id = v.vendor_id
     group by vendor_state, vendor_name
),
top_in_state as
(
     select vendor_state, max(sum_of_invoices) as sum_of_invoices
     from summary
     group by vendor_state
)
select s.vendor_state, s.vendor_name, t.sum_of_invoices
from summary s, top_in_state t
where s.vendor_state = t.vendor_state and s.sum_of_invoices = t.sum_of_invoices
order by s.vendor_state;

 

The result set

  • 大小: 12.9 KB
  • 大小: 9.7 KB
分享到:
评论

相关推荐

    《Pro Oracle SQL》Chapter 10.2.5 Applying Subquery Factoring to PL/SQL

    在Oracle SQL的世界里,"Subquery Factoring"(也称为"Common Table Expression"或CTE)是一种高效且灵活的查询构造技术,它允许我们将复杂的子查询重用并简化查询语句。这一概念在《Pro Oracle SQL》的第10章第2.5...

    《Pro Oracle SQL》Chapter 9 -- 9.11 Subquery Factoring

    在9.11章节中,主要讲解了Subquery Factoring,也被称为Common Table Expression(CTE),这是一个非常实用且强大的SQL特性,能够帮助我们编写更清晰、可读性更强的查询语句。 Subquery Factoring,即子查询因子化...

    《Pro Oracle SQL》Chapter 10 Subquery Factoring --10.1 Standard Usage

    《Pro Oracle SQL》第10章"Subquery Factoring"深入探讨了这一主题,尤其是10.1节“Standard Usage”部分,讲解了子查询在实际应用中的常见用法。在本章节中,作者可能详细阐述了如何利用子查询来优化SQL语句,提高...

    Pro Oracle SQL

    Readers should also want to learn about Oracle Database features such as analytic queries, the MODEL clause, and subquery refactoring that are designed to help developers and DBAs exert control over ...

    Laravel开发-eloquent-subquery-magic

    Eloquent Subquery Magic是Eloquent ORM的一个扩展,它增强了原生Eloquent的功能,尤其是对子查询的支持。这个扩展允许开发者在查询构建器中更灵活地使用子查询,从而实现复杂的数据筛选和分析。 1. 子查询基础 子...

    oracle advanced sql 高级SQL教程 ORACLE官方教材

    Using a Subquery in the FROM Clause 4-10 Scalar Subquery Expressions 4-11 Scalar Subqueries: Examples 4-12 Correlated Subqueries 4-14 Using Correlated Subqueries 4-16 Using the EXISTS Operator 4-18 ...

    Apply a Like Filter through an IN Subquery

    假设我们想查看以"S"或"A"开头的城市的年度销售。如果使用“LIKE”过滤器,逻辑查询语句可能是这样的: ```sql SELECT Time."Year" saw_0, "Facts: Sales"."Store Sales" saw_1 FROM Foodmart WHERE Geography....

    微软内部资料-SQL性能优化5

    A qualified operation is one that affects only specific rows that satisfy the conditions of a WHERE clause, as opposed to accessing the whole table. An index can have multiple node levels An index ...

    SQLPrompt_7.3.0.768

    OPTION clause is now placed on a new line (UserVoice and forum post) Improved consistency of formatting across statements Close button added to formatting options window Support for multi-lined user ...

    Delphi7.1 Update

    visit the Borland Registered User web site to obtain a localized readme file that may contain important late- breaking information not included in this readme file.IMPORTANT: Delphi must be closed ...

    sql with as用法详解

    一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到。有的时候,是为了让SQL语句的可读性更高些,也有可能是在UNION ...

    SQL Server中with as使用介绍

     WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到。有的时候,是为了让SQL语句的可读性更高些,也有可能是在UNION ALL的不同部分,...

    关于SQL中CTE(公用表表达式)(Common Table Expression)的总结

    一.WITH AS的含义 WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个SQL片断,该SQL片断会被整个SQL语句所用到。有的时候,是为了让SQL语句的可读性更高些,也有可能是在UNION ...

    《Pro Oracle SQL》CHAPTER2--2.8 Subquery Unnesting

    在2.8节“子查询展开(Subquery Unnesting)”中,我们将会深入探讨Oracle SQL中一种优化查询性能的重要策略。这一部分的知识点主要包括以下几个方面: 1. **子查询的基本概念**: 子查询是SQL语句中嵌套的另一个...

    黄远邦_复杂而有趣的ORACLE优化案例集锦

    - 对于 `(A.id IN x_subquery OR A.id NOT IN y_subquery)`,由于x是y的子集,可以将其改写为 `A.id NOT IN (y_subquery MINUS x_subquery)`。 - 类似地,对于 `(A.id IN z_subquery OR A.id NOT IN w_subquery)`,...

    linq to sql 学习笔记

    - `Subquery`:子查询,查找客户中订单数量超过 5 个的客户 ID。 - `In`:检查数据是否存在于给定的集合中。 5. `DataContext` 类:它是 LINQ to SQL 的核心,负责将 LINQ 查询转换为 SQL 语句,与数据库交互,并...

    ocp认证培训资料

    Oracle 9i引入了新的SQL功能,如Analytic Functions和Recursive Subquery factoring,这些也会在培训中涉及。 3. **PL/SQL编程**:PL/SQL是Oracle的内置过程语言,用于编写存储过程、函数、触发器等。掌握PL/SQL...

    sql server 2000数据库常有查询语句

    SELECT a.card_code, SUM(DECODE(a.q, 1, a.bal, 0)) AS q1, SUM(DECODE(a.q, 2, a.bal, 0)) AS q2, SUM(DECODE(a.q, 3, a.bal, 0)) AS q3, SUM(DECODE(a.q, 4, a.bal, 0)) AS q4 FROM t_change_lca GROUP BY a....

    14.4、Subquery&Casewhen(三).mp4

    Python零基础10天进阶班【14课程:SQL数据分析及变更(下)】

    14.3、Subquery&Casewhen(二).mp4

    Python零基础10天进阶班【14课程:SQL数据分析及变更(下)】

Global site tag (gtag.js) - Google Analytics