0

在 SQL Server 中使用 RECURSIVE CTE 时,GROUP BY不应使用 等LEFT OUTER JOINORDER BY

如何将此 ORACLE 查询转换为 SQL Server?

SELECT *
  FROM (SELECT COALESCE(a.tree_id, ' ') MNUID,
                COALESCE(a.tree_nm, ' ') MNUNM,
                COALESCE(A.TREE_LEV_CD, ' ') MNULEVCD,
                COALESCE(A.UP_TREE_ID, ' ') UPMNUID,
                COALESCE(A.TREE_CD, ' ') TREECD,
                COALESCE(A.TREE_LRK_RUF_ID, ' ') SCEXRSSEQVL,
                CASE WHEN B.SCH_CNT=NULL THEN 0 ELSE B.SCH_CNT END AS SCHCNT
        FROM SC_TREE_MASTER A
        LEFT OUTER JOIN
            (SELECT C.TREE_LRK_RUF_ID, COUNT(SCH_ID) SCH_CNT
                FROM SC_TREE_MASTER C
                LEFT OUTER JOIN AL_SCHEDULE_MAPPING D
                ON C.TREE_LRK_RUF_ID = D.PRA_WF_ID
                WHERE C.TREE_BJ_CD = '01'
                AND C.TREE_CD = '04'
                GROUP BY TREE_LRK_RUF_ID) B
        ON (A.TREE_BJ_CD = '01' AND A.TREE_LRK_RUF_ID = B.TREE_LRK_RUF_ID)
        ORDER BY MNULEVCD, SCEXRSSEQVL)
 START WITH UPMNUID = 'TR00000000'
CONNECT BY PRIOR MNUID = UPMNUID
 ORDER SIBLINGS BY MNUID;

我试过了。

但是发生了错误。

WITH TEMP_TAB(MNUID, MNUNM, MNULEVCD, UPMNUID, TREECD, TREE_LRK_RUF_ID, SORT, RECURSIVE_LEVEL) AS            
 (SELECT TREE_ID,
         TREE_NM,
         TREE_LEV_CD,
         UP_TREE_ID,
         TREE_CD,
         TREE_LRK_RUF_ID,
         CONCAT('-',TREE_ID) AS SORT,
         1 AS RECURSIVE_LEVEL
    FROM SC_TREE_MASTER 
   WHERE TREE_BJ_CD = '01'
     AND UP_TREE_ID = 'TR00000000'
  UNION ALL
  SELECT A.TREE_ID,
         A.TREE_NM,
         A.TREE_LEV_CD,
         A.UP_TREE_ID,
         A.TREE_CD,
         A.TREE_LRK_RUF_ID,
         CONCAT(B.SORT,'/',A.TREE_ID) AS SORT,
         (RECURSIVE_LEVEL + 1) AS RECURSIVE_LEVEL
    FROM SC_TREE_MASTER A, TEMP_TAB B
   WHERE A.TREE_BJ_CD = '01'
     AND B.MNUID =A.UP_TREE_ID 
 )
SELECT COALESCE(M.TREE_ID, ' ') MNUID,
       COALESCE(M.TREE_NM, ' ') MNUNM,
       COALESCE(M.TREE_LEV_CD, ' ') MNULEVCD,
       COALESCE(M.UP_TREE_ID, ' ') UPMNUID,
       COALESCE(M.TREE_CD, ' ') TREECD,
       COALESCE(M.TREE_LRK_RUF_ID, ' ') TREE_LRK_RUF_ID
FROM TEMP_TAB M                
LEFT OUTER JOIN (SELECT C.TREE_LRK_RUF_ID, COUNT(SCH_ID) SCH_CNT
                  FROM SC_TREE_MASTER C
                  LEFT OUTER JOIN AL_SCHEDULE_MAPPING D
                    ON C.TREE_LRK_RUF_ID = D.PRA_WF_ID
                 WHERE C.TREE_BJ_CD = '01'
                   AND C.TREE_CD = '04'
                 GROUP BY TREE_LRK_RUF_ID) N
ON M.TREE_LRK_RUF_ID = N.TREE_LRK_RUF_ID 
ORDER BY SORT;

递归查询“TEMP_TAB”的“SORT”列中的锚点和递归部分之间的类型不匹配。

另一个问题:如何将connect byORACLE 中的所有数据库系统转换为 ANSI?

不可以吗??

我在下面添加了这个可以在 POSTGRESQL 中使用的查询。

我认为这个查询与 SQL Server RECURSIVE-CTE 查询类似,但在 SQL Server 中,它不起作用。

WITH RECURSIVE TEMP_TAB(TREE_ID,
         TREE_NM,
         TREE_LEV_CD,
         UP_TREE_ID,
         TREE_CD,
         TREE_LRK_RUF_ID,
         SORT ,                         
         RECURSIVE_LEVEL) AS            
 (SELECT TREE_ID,
         TREE_NM,
         TREE_LEV_CD,
         UP_TREE_ID,
         TREE_CD,
         TREE_LRK_RUF_ID,
         CONCAT('-',TREE_ID) ,
         1 RECURSIVE_LEVEL
    FROM SC_TREE_MASTER 
   WHERE TREE_BJ_CD = '01'
     AND UP_TREE_ID = 'TR00000000'
  UNION ALL
  SELECT A.TREE_ID,
         A.TREE_NM,
         A.TREE_LEV_CD,
         A.UP_TREE_ID,
         A.TREE_CD,
         A.TREE_LRK_RUF_ID,
         CONCAT(B.SORT,'/',A.TREE_ID),
         RECURSIVE_LEVEL + 1
    FROM SC_TREE_MASTER A, TEMP_TAB B
   WHERE A.TREE_BJ_CD = '01'
     AND B.TREE_ID =A.UP_TREE_ID 
 )
SELECT COALESCE(M.TREE_ID, ' ') MNUID,
       COALESCE(M.TREE_NM, ' ') MNUNM,
       COALESCE(M.TREE_LEV_CD, ' ') MNULEVCD,
       COALESCE(M.UP_TREE_ID, ' ') UPMNUID,
       COALESCE(M.TREE_CD, ' ') TREECD,
       COALESCE(M.TREE_LRK_RUF_ID, ' ') SCEXRSSEQVL,
       COALESCE(N.SCH_CNT, 0)
FROM TEMP_TAB M                
LEFT OUTER JOIN (SELECT C.TREE_LRK_RUF_ID, COUNT(SCH_ID) SCH_CNT
                  FROM SC_TREE_MASTER C
                  LEFT OUTER JOIN AL_SCHEDULE_MAPPING D
                    ON C.TREE_LRK_RUF_ID = D.PRA_WF_ID
                 WHERE C.TREE_BJ_CD = '01'
                   AND C.TREE_CD = '04'
                 GROUP BY TREE_LRK_RUF_ID) N
ON M.TREE_LRK_RUF_ID = N.TREE_LRK_RUF_ID 
ORDER BY SORT;
4

1 回答 1

0
WITH TEMP_TAB(TREE_ID,
         TREE_NM,
         TREE_LEV_CD,
         UP_TREE_ID,
         TREE_CD,
         TREE_LRK_RUF_ID,
         SORT,                         
         RECURSIVE_LEVEL) AS            
 (SELECT TREE_ID,
         TREE_NM,
         TREE_LEV_CD,
         UP_TREE_ID,
         TREE_CD,
         TREE_LRK_RUF_ID,
         CAST(CONCAT('-',TREE_ID) AS VARCHAR(100)),
         1 RECURSIVE_LEVEL
    FROM SC_TREE_MASTER 
   WHERE TREE_BJ_CD = '01'
     AND UP_TREE_ID = 'TR00000000'
  UNION ALL
  SELECT A.TREE_ID,
         A.TREE_NM,
         A.TREE_LEV_CD,
         A.UP_TREE_ID,
         A.TREE_CD,
         A.TREE_LRK_RUF_ID,
         CAST(CONCAT(B.SORT,'/',A.TREE_ID) AS VARCHAR(100)),
         RECURSIVE_LEVEL + 1
    FROM SC_TREE_MASTER A, TEMP_TAB B
   WHERE A.TREE_BJ_CD = '01'
     AND B.TREE_ID =A.UP_TREE_ID 
 )
SELECT COALESCE(M.TREE_ID, ' ') MNUID,
       COALESCE(M.TREE_NM, ' ') MNUNM,
       COALESCE(M.TREE_LEV_CD, ' ') MNULEVCD,
       COALESCE(M.UP_TREE_ID, ' ') UPMNUID,
       COALESCE(M.TREE_CD, ' ') TREECD,
       COALESCE(M.TREE_LRK_RUF_ID, ' ') SCEXRSSEQVL,
       COALESCE(N.SCH_CNT, 0)
FROM TEMP_TAB M                
LEFT OUTER JOIN (SELECT C.TREE_LRK_RUF_ID, COUNT(SCH_ID) SCH_CNT
                  FROM SC_TREE_MASTER C
                  LEFT OUTER JOIN AL_SCHEDULE_MAPPING D
                    ON C.TREE_LRK_RUF_ID = D.PRA_WF_ID
                 WHERE C.TREE_BJ_CD = '01'
                   AND C.TREE_CD = '04'
                 GROUP BY TREE_LRK_RUF_ID) N
ON M.TREE_LRK_RUF_ID = N.TREE_LRK_RUF_ID 
ORDER BY SORT;
于 2014-05-22T07:45:54.743 回答