WITH OrganizationChart (Id, [Name], [Level], ManagerID) AS (
SELECT
Id, [Name], 0 as [Level], ManagerID
FROM
[dbo].[Employees] emp
WHERE
ManagerID IS NULL
UNION ALL
SELECT
emp.ID, emp.[Name], [Level] + 1, emp.ManagerID
FROM [dbo].[Employees] emp
INNER JOIN OrganizationChart d ON
emp.ManagerID = d.Id
)
SELECT [Id], [Name], [Level], [ManagerID] INTO #ctedemo from OrganizationChartCREATE VOLATILE TABLE ctedemo AS (WITH RECURSIVE OrganizationChart (Id, Name, Level, ManagerID) AS (
SELECT
Id, Name, 0 as Level, ManagerID
FROM
TEST.Employees emp
WHERE
ManagerID IS NULL
UNION ALL
SELECT
emp.ID, emp.Name, Level + 1, emp.ManagerID
FROM TEST.Employees emp
INNER JOIN OrganizationChart d ON
emp.ManagerID = d.Id
) SELECT Id, Name, Level, ManagerID from OrganizationChart) WITH DATA NO PRIMARY INDEX
ON COMMIT PRESERVE ROWSDECLARE GLOBAL TEMPORARY TABLE SESSION.ctedemo(ID int, Name varchar(50), Level int, ManagerID int) on commit preserve rows with replace not logged;
INSERT INTO SESSION.ctedemo(ID, Name, Level, ManagerID) WITH OrganizationChart (Id, Name, Level, ManagerID) AS (
SELECT
Id, Name, 0 as Level, ManagerID
FROM
Employees emp
WHERE
ManagerID IS NULL
UNION ALL
SELECT
emp.ID, emp.Name, Level + 1, emp.ManagerID
FROM Employees emp, OrganizationChart d
where
emp.ManagerID = d.Id
)
SELECT Id, Name, Level, ManagerID from OrganizationChartSELECT * from #ctedemoSELECT * from ctedemoSELECT * FROM SESSION.ctedemo|
ID |
Name |
ManagerID |
|
1 |
John |
NULL |
|
2 |
Jim |
1 |
|
3 |
Jane |
1 |
|
4 |
Tom |
3 |
|
5 |
Bob |
4 |
|
6 |
Rob |
3 |
|
7 |
Mike |
1 |
|
ID |
Name |
Level |
ManagerID |
|
1 |
John |
0 |
NULL |
|
2 |
Jim |
1 |
1 |
|
3 |
Jane |
1 |
1 |
|
7 |
Mike |
1 |
1 |
|
4 |
Tom |
2 |
3 |
|
6 |
Rob |
2 |
3 |
|
5 |
Bob |
3 |
4 |
When Tableau queries a data source, Custom SQL is passed in a query like the following:
SELECT TOP 1 * from ( [All your Custom SQL here] ) as [TableauSQL]
SQL Server and Teradata do not support CTE use in a subquery, so the result is an error.
Initial SQL is not passed in a subquery, so can be used for a CTE.
To voice your support for the inclusion of this feature in a future product release, add your vote to the following Idea:
CTE compatibility with SQL Server (v. 10.0)
001453743

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.