Vannessa
Vannessa

Reputation: 183

SQL incorrect syntax in tableau

I have this error message when I insert my SQL query in Tableau. How to solve this issue?

Is it SQL Server currently does not allow CTE's inside a subquery??

Below is the Tableau output when I insert query in

[Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near the keyword 'WITH'.

[Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.

[Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near ')'.

Below is my current CTE Query (recursive)

WITH shiftHours AS (
    SELECT RowID,
    y.EMPLOYEENAME AS EMPLOYEENAME,
    -- flatten the first hour to remove the minutes and get the initial current hour
    DATEADD(hour, DATEDIFF(hour, 0, ShiftA_Start), 0) AS currentHour,
    ShiftA_Start,
    ShiftA_End,
    DATEPART(hour, ShiftA_Start) AS hourOrdinal,
    -- determine how much of the first hour is applicable. if it is minute 0 then the whole hour counts
    CAST(CASE 
        WHEN DATEADD(hour, DATEDIFF(hour, 0, ShiftA_Start), 0) = DATEADD(hour, DATEDIFF(hour, 0, ShiftA_End), 0) THEN DATEDIFF(minute, ShiftA_Start, ShiftA_End) / 60.0
        WHEN DATEPART(minute, ShiftA_Start) = 0 THEN 1.0
        ELSE (60 - DATEPART(minute, ShiftA_Start)) / 60.0
    END AS DECIMAL(5,3)) AS hourValue
FROM (
    -- use a ROW_NUMBER() to generate row IDs for the shifts to ensure each row is unique once it gets to the pivot
    SELECT ROW_NUMBER() OVER(ORDER BY ShiftA_Start, ShiftA_End) AS RowID,
    EMPLOYEENAME,
        ShiftA_Start,
        ShiftA_End
    FROM (
        -- this is where the data gets pulled from the source table and where the data types are converted from string to DATETIME
        SELECT 
        EMPLOYEENAME,
        CONVERT(DATETIME, LEFT(SHIFTA_start, 17), 103) AS ShiftA_Start,
            CONVERT(DATETIME, LEFT(SHIFTA_end, 17), 103) AS ShiftA_End
        from [TableName].[dbo].[TMS_People]
        where
        CONVERT(DATETIME, LEFT(SHIFTA_START, 17), 103) IS NOT NULL AND CONVERT(DATETIME, LEFT(SHIFTA_END, 17), 103) IS NOT NULL
        AND CONVERT(DATETIME, LEFT(SHIFTA_START, 17), 103) != CONVERT(DATETIME, LEFT(SHIFTA_END, 17), 103)


        -- this is also where you would add any filtering from the source table such as date ranges
    ) x
) AS y


UNION ALL

SELECT RowID,
EMPLOYEENAME,
    -- add an hour to the currentHour each time the recursive CTE is called
    DATEADD(hour, 1, currentHour) AS currentHour,
    ShiftA_Start,
    ShiftA_End,
    DATEPART(hour, DATEADD(hour, 1, currentHour)) AS hourOrdinal,
    CAST(CASE
        -- when this is the last time period determine the amount of the hour that is applicable
        WHEN DATEADD(hour, 2, currentHour) > ShiftA_End THEN DATEPART(minute, ShiftA_End) / 60.0
        ELSE 1
    END AS DECIMAL(5,3)) AS hourValue
from shiftHours

-- contine recursion until the next hour is after the ShiftEnd
WHERE DATEADD(hour, 1, currentHour) < ShiftA_End
)
    SELECT *
FROM (
    SELECT RowID,
EMPLOYEENAME,
    ShiftA_Start,
    ShiftA_End,
    hourValue,
    hourOrdinal
from shiftHours

    ) AS t
    PIVOT (
     SUM(hourValue)
    FOR hourOrdinal IN ([0], [1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23])
) AS pvt
OPTION (MAXRECURSION 0);

Upvotes: 1

Views: 2173

Answers (1)

sandeep rawat
sandeep rawat

Reputation: 4957

Tableau do not support SQL server CTE feature .

You should try to implement same logic using view or sub query or Stored proc/table valued function .

Please go throw below thread CTE (SQL Server) are not sported in Tableau.

https://community.tableau.com/thread/105965 .

View Sample code. for CTE

If we have user table (userId, userName, managerId)

CREATE VIEW vUSER AS
    WITH UserCTE AS (
    SELECT userId, userName, managerId,0 AS steps
    FROM dbo.Users
    WHERE userId = null

   UNION ALL
         SELECT mgr.userId, mgr.userName, mgr.managerId, usr.steps +1 AS        steps
     FROM UserCTE AS usr
     INNER JOIN dbo.Users AS mgr
     ON usr.managerId = mgr.userId
    )
   SELECT * FROM UserCTE ;

Create an sp ie MyProc and put all your query in it ie

  CREATE procedure  dbo.MyProc AS
    WITH UserCTE AS (
    SELECT userId, userName, managerId,0 AS steps
    FROM dbo.Users
    WHERE userId = null

   UNION ALL
         SELECT mgr.userId, mgr.userName, mgr.managerId, usr.steps +1 AS        steps
     FROM UserCTE AS usr
     INNER JOIN dbo.Users AS mgr
     ON usr.managerId = mgr.userId
    )
   SELECT * FROM UserCTE ;

2 Create a link server using sp_addlinkedserver ie i am telling is Local

3 Call that sp in your view using openquery.

     CREATE VIEW dbo.MyView
       AS(
         SELECT        *
               FROM openquery(Local,'exec mydb.dbo.MyProc'))

http://capnjosh.com/blog/how-to-call-a-stored-procedure-from-a-view-in-sql-server/

Upvotes: 2

Related Questions