针对公用表表达式(CTE)的T-SQL JOIN

时间:2011-12-06 16:29:21

标签: sql tsql

是否可以对公用表表达式执行JOIN子查询?如果没有,那么有人可以让我知道如何执行我在下面尝试做的事情吗?例子很棒。

例如:

LEFT JOIN (

            ;WITH [UserDefined]
                AS (SELECT *, -- Make sure we get only the latest revision.
                        ROW_NUMBER() OVER (PARTITION BY [ID]
                                                ORDER BY [RevisionNumber] DESC) AS RN
                    FROM [syn_Change])

            SELECT [UserDefined].[ID]
                ,[UserDefined].[ChangeNumber]
                ,[UserDefined].[Usr_CoResponsibility]
                ,[UserDefined].[Usr_StarFlowStatus]

            FROM [UserDefined]
            WHERE (RN = 1) 

            ) [UserColumns]
ON [UserColumns].[ChangeNumber] = [CTE].[ChangeNumber]

以下是我的完整查询:

;WITH CTE
    AS (SELECT *, -- Make sure we get only the latest revision.
            ROW_NUMBER() OVER (PARTITION BY [ItemID]
                                    ORDER BY [RevisionNumber] DESC) AS RN
        FROM [dw_Change])

SELECT [CTE].[ItemID]
        ,[CTE].[ViewID]
        ,[CTE].[FolderItemID]
        ,[CTE].[RevisionNumber]
        ,[CTE].[ChangeNumber]
        ,[CTE].[Synopsis]
        ,[CTE].[Description]
        ,[CTE].[EnteredOn]
        ,[CTE].[Responsibility]
        --,[UserColumns].[Usr_CoResponsibility]
        --,[UserColumns].[Usr_StarFlowStatus]
        ,[CTE].[Status] -- This will display the human name on the front-end with code.
        ,[Users].[F7] AS [ResponsibilityName]
        ,[GroupName].[Name] AS [AppGroupName]
        ,[AppName].[Name] AS [AppName]
FROM CTE
LEFT JOIN [S3] [Users] ON [Users].[F0] = [CTE].[Responsibility]
LEFT JOIN (SELECT [Name], [ViewID]
            FROM [dw_Folder]
            WHERE ([FolderItemID] = -1)) [GroupName]
ON [GroupName].[ViewID] = [CTE].[ViewID]

LEFT JOIN (SELECT [Name], [ItemID]
            FROM [dw_Folder]
            WHERE ([FolderItemID] <> -1)) [AppName]
ON [AppName].[ItemID] = [CTE].[FolderItemID]

LEFT JOIN (

            ;WITH [UserDefined]
                AS (SELECT *, -- Make sure we get only the latest revision.
                        ROW_NUMBER() OVER (PARTITION BY [ID]
                                                ORDER BY [RevisionNumber] DESC) AS RN
                    FROM [syn_Change])

            SELECT [UserDefined].[ID]
                ,[UserDefined].[ChangeNumber]
                ,[UserDefined].[Usr_CoResponsibility]
                ,[UserDefined].[Usr_StarFlowStatus]

            FROM [UserDefined]
            WHERE (RN = 1) 

            ) [UserColumns]
ON [UserColumns].[ChangeNumber] = [CTE].[ChangeNumber]

WHERE (RN = 1)

非常感谢!

5 个答案:

答案 0 :(得分:39)

在定义CTE时,您在查询的其余任何内容之前执行此操作。所以你不能写:

LEFT JOIN (
  ;WITH CTE
  ...
)

快速地,原因人们将;放在WITH前面是因为所有先前的陈述都需要终止。如果开发人员习惯于使用;终止所有SQL语句,那么就没有必要,但我离题了......

您可以像这样编写多个CTE:

WITH SomeCTE AS (
  SELECT ...
  FROM ...
), AnotherCTE AS (
  SELECT ...
  FROM ...
)
SELECT *
FROM SomeCTE LEFT JOIN
     AnotherCTE ON ...
;

答案 1 :(得分:3)

如果你有多个CTE,他们需要在你的陈述的开头(以逗号分隔,只有一个;WITH来开始CTE列表):

;WITH CTE AS (......),
 [UserDefined] AS (.......)
SELECT.....

然后您可以在SELECT语句中使用两者(甚至两个以上)。

答案 2 :(得分:3)

需要首先声明多个CTE。例如:

WITH CTE_1 AS
(
    ....
),
CTE_2 AS
(
    ...
)

SELECT        *
FROM          FOO
LEFT JOIN     CTE_1
LEFT JOIN     CTE_2

答案 3 :(得分:1)

我相信你可以拥有多个CTE;你只需把它们放在最上面。

请参阅here

  

但是,您可以在WITH关键字后面定义多个CTE,方法是用逗号分隔每个CTE。

答案 4 :(得分:1)

;
WITH
  RANKED_CTE AS
(
  SELECT
    *,
    ROW_NUMBER() OVER (PARTITION BY [ItemID] ORDER BY [RevisionNumber] DESC) AS RN
  FROM
    [dw_Change]
)
,
  CTE AS
(
  SELECT
    *
  FROM
    RANKED_CTE
  WHERE
    RN = 1
)
,
  GroupName AS
(
  SELECT
    [Name], [ViewID]
  FROM
    [dw_Folder]
  WHERE
    ([FolderItemID] = -1)
)
,
  AppName AS
(
  SELECT
    [Name], [ItemID]
  FROM
    [dw_Folder]
  WHERE
    ([FolderItemID] <> -1)
)
SELECT [CTE].[ItemID]
        ,[CTE].[ViewID]
        ,[CTE].[FolderItemID]
        ,[CTE].[RevisionNumber]
        ,[CTE].[ChangeNumber]
        ,[CTE].[Synopsis]
        ,[CTE].[Description]
        ,[CTE].[EnteredOn]
        ,[CTE].[Responsibility]
        --,[UserColumns].[Usr_CoResponsibility]
        --,[UserColumns].[Usr_StarFlowStatus]
        ,[CTE].[Status] -- This will display the human name on the front-end with code.
        ,[Users].[F7] AS [ResponsibilityName]
        ,[GroupName].[Name] AS [AppGroupName]
        ,[AppName].[Name] AS [AppName]
FROM
          CTE
LEFT JOIN [S3] [Users]  ON [Users].[F0] = [CTE].[Responsibility]
LEFT JOIN [GroupName]   ON [GroupName].[ViewID] = [CTE].[ViewID]
LEFT JOIN [AppName]     ON [AppName].[ItemID] = [CTE].[FolderItemID]