Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6901975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:44:27+00:00 2026-05-27T07:44:27+00:00

Is it possible to perform a JOIN sub query against a Common Table Expression?

  • 0

Is it possible to perform a JOIN sub query against a Common Table Expression? If not, then can someone let me know how to perform what I am trying to do below? Examples would be excellent.

For example:

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]

Here is my full query:

;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)

Thanks so much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:44:28+00:00Added an answer on May 27, 2026 at 7:44 am

    When you define a CTE you’re doing so before any of the rest of the query. So you can’t write:

    LEFT JOIN (
      ;WITH CTE
      ...
    )
    

    As a quick aside, the reason people put ; in front of WITH is because all previous statements need to be terminated. If developers could get in the habit of terminating all SQL statements with ; then it wouldn’t be necessary, but I digress…

    You can write multiple CTEs like so:

    WITH SomeCTE AS (
      SELECT ...
      FROM ...
    ), AnotherCTE AS (
      SELECT ...
      FROM ...
    )
    SELECT *
    FROM SomeCTE LEFT JOIN
         AnotherCTE ON ...
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it even possible to perform address (physical, not e-mail) validation? It seems like
In SQL Server is it possible to perform table partition without having to recode
What are possible reasons why NHibernate does not perform delete operation? public bool Delete(MyType
can i perform select single node from IXMLDOMElement ,if its possible give me one
using Entity Framework 4.1 is it possible to perform a left outer join with
Is it possible to perform a named-group match in Perl's regex syntax as with
Is it possible to perform a global reversed-find on NHibernate-managed objects? Specifically, I have
Is it possible to perform some custom processing when Windsor instantiates a type? Something
Is it possible to perform an action when a UITableViewCell is highlighted? For example
Is it possible to use a jQuery ajax call to perform Forms Authentication with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.