您可以为公用表表达式创建嵌套的WITH子句吗?


Answers:


301

尽管不是严格嵌套的,但是您可以使用公用表表达式在后续查询中重用先前的查询。

为此,您要查找的语句形式为

WITH x AS 
(
    SELECT * FROM MyTable
), 
y AS 
(
    SELECT * FROM x
)
SELECT * FROM y

2
非常感谢你。我能够在Oracle中做到这一点:用J AS(从双重选择1中的一个),Q AS(从J选择2个中的J. *,2从J中选择2个)SELECT * FROM Q
Jason TEPOORTEN

5
这不是嵌套
共生

13
从本质上讲,该帖子意味着您不能这样做,但这不是一个大问题。
peterh-恢复莫妮卡

2
是的,这是一个可以接受的答案,因为我试图通过嵌套实现的结果还是反过来给了我
Joe Phillips

2
声明这不是嵌套的,只是因为查询2不在查询1的括号内,听起来像一个弱参数。我认为它是嵌套的(不是递归嵌套的),因为查询2使用查询1的结果,查询1的结果也与嵌套一起发生。是否定义了仅当孩子在其父括号(或类似的括号)内时才可以嵌套?
克里斯蒂安·韦斯特贝克

11

您可以执行以下操作,这称为递归查询:

WITH y
AS
(
  SELECT x, y, z
  FROM MyTable
  WHERE [base_condition]

  UNION ALL

  SELECT x, y, z
  FROM MyTable M
  INNER JOIN y ON M.[some_other_condition] = y.[some_other_condition]
)
SELECT *
FROM y

您可能不需要此功能。为了更好地组织查询,我做了以下操作:

WITH y 
AS
(
  SELECT * 
  FROM MyTable
  WHERE [base_condition]
),
x
AS
(
  SELECT * 
  FROM y
  WHERE [something_else]
)
SELECT * 
FROM x

7

With无法嵌入工作,但可以连续工作

;WITH A AS(
...
),
B AS(
...
)
SELECT *
FROM A
UNION ALL
SELECT *
FROM B

编辑 修复了语法...

另外,看看下面的例子

SQLFiddle演示


0

这些答案非常好,但是就正确订购商品而言,最好还是看看这篇文章 http://dataeducation.com/dr-output-or-how-i-learned-to-stop担心和爱合并

这是他的查询示例。

WITH paths AS ( 
    SELECT 
        EmployeeID, 
        CONVERT(VARCHAR(900), CONCAT('.', EmployeeID, '.')) AS FullPath 
    FROM EmployeeHierarchyWide 
    WHERE ManagerID IS NULL

    UNION ALL

    SELECT 
        ehw.EmployeeID, 
        CONVERT(VARCHAR(900), CONCAT(p.FullPath, ehw.EmployeeID, '.')) AS FullPath 
    FROM paths AS p 
        JOIN EmployeeHierarchyWide AS ehw ON ehw.ManagerID = p.EmployeeID 
) 
SELECT * FROM paths order by FullPath

我最初的问题从未说过关于将数据联合在一起的任何事情。可能很容易加入数据
Joe Phillips

0

我试图测量事件之间的时间,除了哪一个条目在开始和结束之间具有多个过程。在其他单行过程中,我需要这样做。

我在Nth cte中使用带内部联接的select作为我的select语句。我需要在第二个cte中提取X上的开始日期和Y上的结束日期,并使用1作为ID值进行左连接以将它们放在一行上。

为我工作,希望对您有所帮助。

cte_extract
as 
(
    select ps.Process as ProcessEvent
        , ps.ProcessStartDate 
        , ps.ProcessEndDate 
        -- select strt.*
    from dbo.tbl_some_table ps 
    inner join (select max(ProcessStatusId) ProcessStatusId 
                    from dbo.tbl_some_table 
                where Process = 'some_extract_tbl' 
                and convert(varchar(10), ProcessStartDate, 112) < '29991231'
                ) strt on strt.ProcessStatusId = ps.ProcessStatusID
), 
cte_rls
as 
(
    select 'Sample' as ProcessEvent, 
     x.ProcessStartDate, y.ProcessEndDate  from (
    select 1 as Id, ps.Process as ProcessEvent
        , ps.ProcessStartDate 
        , ps.ProcessEndDate
        -- select strt.*
    from dbo.tbl_some_table ps 
    inner join (select max(ProcessStatusId) ProcessStatusId 
                    from dbo.tbl_some_table 
                where Process = 'XX Prcss' 
                and convert(varchar(10), ProcessStartDate, 112) < '29991231'
                ) strt on strt.ProcessStatusId = ps.ProcessStatusID
    ) x
    left join (
        select 1 as Id, ps.Process as ProcessEvent
            , ps.ProcessStartDate 
            , ps.ProcessEndDate
            -- select strt.*
        from dbo.tbl_some_table ps 
        inner join (select max(ProcessStatusId) ProcessStatusId
                    from dbo.tbl_some_table 
                    where Process = 'YY Prcss Cmpltd' 
                    and convert(varchar(10), ProcessEndDate, 112) < '29991231'
                    ) enddt on enddt.ProcessStatusId = ps.ProcessStatusID
            ) y on y.Id = x.Id 
),

....其他ctes


0

不支持嵌套的“ With”,但是您始终可以将第二个With用作子查询,例如:

WITH A AS (
                --WITH B AS ( SELECT COUNT(1) AS _CT FROM C ) SELECT CASE _CT WHEN 1 THEN 1 ELSE 0 END FROM B --doesn't work
                SELECT CASE WHEN count = 1 THEN 1 ELSE 0 END AS CT FROM (SELECT COUNT(1) AS count FROM dual)
                union all
                select 100 AS CT from dual
           )
              select CT FROM A

-1

我们可以创建嵌套的cte.please请参见下面的cte示例

;with cte_data as 
(
Select * from [HumanResources].[Department]
),cte_data1 as
(
Select * from [HumanResources].[Department]
)

select * from cte_data,cte_data1

4
您参加聚会有点晚了;)
乔·菲利普斯

4
那是连续的CTE,而不是嵌套的CTE
Meower68 '18年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.