2016-05-05 7 views
1

Ich habe diese Fehlermeldung, wenn ich meine SQL-Abfrage in Tableau einfügen. Wie kann ich dieses Problem lösen?SQL falsche Syntax in Tableau

Ist es SQL Server derzeit CTEs in einer Unterabfrage nicht erlaubt ??

Unten ist die Tableau Ausgabe, wenn ich Abfrage einfügen in

[Microsoft] [SQL Server Native Client 11.0] [SQL Server] Falsche Syntax nahe dem Schlüsselwort 'MIT'.

[Microsoft] [SQL Server Native Client 11.0] [SQL Server] Falsche Syntax in der Nähe des Schlüsselwortes 'mit'. Wenn diese Anweisung eine gemeinsame Tabelle Ausdruck, eine XMLNamespaces-Klausel oder eine Änderungsverfolgungskontext -Klausel ist, muss die vorherige Anweisung mit einem Semikolon abgeschlossen werden.

[Microsoft] [SQL Server Native Client 11.0] [SQL Server] Falsche Syntax in der Nähe von ')'.

Im Folgenden finden Sie meine aktuellen CTE Query (rekursiv)

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

Bitte verwenden Sie die richtigen Tags – e4c5

+1

Ihre Abfrage schreiben – tharif

Antwort

2

Tableau nicht SQL Server CTE Funktion unterstützen.

Sie sollten versuchen, die gleiche Logik mit View oder Sub-Abfrage oder Stored Proc/Tabellenwert-Funktion zu implementieren.

Bitte gehen Sie unter Thread werfen CTE (SQL Server) sind nicht in Tableau sported.

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

Beispielcode anzeigen. für CTE

Wenn wir Benutzertabelle (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 ; 

ein sp dh MyProc Erstellen und setzen alle Ihre Abfrage in das dh

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 Erstellen Sie einen Link Server mit sp_addlinkedserver dh ich erzähle ist Local

3 Rufen Sie das SP in Ihrer Sicht mit 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/

+0

Wie die gleiche Logik Ansicht oder Sub-Abfrage oder gespeicherte proc/Tabellenwertfunktion implementieren ?? Ich mache rekursive, aber ich verstehe, dass Unterabfrage diese Funktion nicht unterstützen kann. – Vannessa

+0

Ich füge den Beispielcode als Antwort hinzu. –

+0

Wie zu einer Ansicht wechseln? Ich habe meinen aktuellen CTE rekursiven Code in der obigen Frage hinzugefügt. Bitte schau es dir an. – Vannessa