2015-08-19 83 views
2

我希望得到家長和他們的孩子數據,意味着先到父母那麼他們的孩子的,下面是查詢,我要轉換爲父母和他們的孩子的Sql父子查詢不工作

SELECT a.FactorTitle, 
     a.FactorCode, 
     a.parentId, 
     c.FactorColumnCode, 
     c.FactorColumnTitle, 
     c.FactorColumnValue, 
     c.isFactorValue, 
     c.FieldType, 

    (SELECT count(*) 
    FROM FactorSetup 
    WHERE parentId=a.FactorCode) AS childCount 
FROM FactorSetup a 
INNER JOIN PDModelSetup b ON a.PDModelCode=b.PDModelCode 
LEFT JOIN FactorColumnSetup c ON a.FactorCode=c.FactorCode 
WHERE a.PDModelCode=2 
    AND c.isFactorValue <> 'Y' 
    AND a.FactorType='4' 

我試試下面查詢

WITH EntityChildren AS 
    (
    SELECT a.FactorTitle, 
      a.FactorCode, 
      a.parentId, 
      c.FactorColumnCode, 
      c.FactorColumnTitle, 
      c.FactorColumnValue, 
      c.isFactorValue, 
      c.FieldType, 

    (SELECT count(*) 
    FROM FactorSetup 
    WHERE parentId=a.FactorCode) AS childCount 
    FROM FactorSetup a 
    LEFT JOIN FactorColumnSetup c ON a.FactorCode=c.FactorCode 
    UNION ALL 
    SELECT a.FactorTitle, 
      a.FactorCode, 
      a.parentId, 
      c.FactorColumnCode, 
      c.FactorColumnTitle, 
      c.FactorColumnValue, 
      c.isFactorValue, 
      c.FieldType, 

    (SELECT count(*) 
    FROM FactorSetup 
    WHERE parentId=a.FactorCode) AS childCount 
    FROM FactorSetup a 

    INNER JOIN EntityChildren e2 ON a.parentId = e2.FactorCode 
    LEFT JOIN FactorColumnSetup c ON a.FactorCode=c.FactorCode 
    ) 

SELECT * FROM EntityChildren 

執行這些查詢後,我得到這個錯誤

Msg 467, Level 16, State 1, Line 1 
GROUP BY, HAVING, or aggregate functions are not allowed in the recursive part of a recursive common table expression 'EntityChildren'. 
Msg 462, Level 16, State 1, Line 1 
Outer join is not allowed in the recursive part of a recursive common table expression 'EntityChildren'. 

然後我改變我的查詢d拔下COUNT(*)

WITH EntityChildren AS 
    (
    SELECT a.FactorTitle, 
      a.FactorCode, 
      a.parentId, 
      c.FactorColumnCode, 
      c.FactorColumnTitle, 
      c.FactorColumnValue, 
      c.isFactorValue, 
      c.FieldType 
    FROM FactorSetup a 
    LEFT JOIN FactorColumnSetup c ON a.FactorCode=c.FactorCode 
    UNION ALL 
    SELECT a.FactorTitle, 
      a.FactorCode, 
      a.parentId, 
      c.FactorColumnCode, 
      c.FactorColumnTitle, 
      c.FactorColumnValue, 
      c.isFactorValue, 
      c.FieldType 
    FROM FactorSetup a 

    INNER JOIN EntityChildren e2 ON a.parentId = e2.FactorCode 
    LEFT JOIN FactorColumnSetup c ON a.FactorCode=c.FactorCode 
    ) 

SELECT * FROM EntityChildren 

然後我得到這個錯誤

Msg 462, Level 16, State 1, Line 1 
Outer join is not allowed in the recursive part of a recursive common table expression 'EntityChildren'. 

回答

0

其實這是由設計,讀取Guidelines for Defining and Using Recursive Common Table Expressions

下列項目不允許在CTE_query_definition 遞歸成員:

  • SELECT DISTINCT
  • GROUP BY
  • HAVING
  • 標量聚合
  • TOP
  • LEFT,RIGHT,OUTER JOIN(INNER JOIN是允許的)
  • 子查詢

您必須將您的LEFT JOIN和COUNT移出CTE查詢,並存儲這些數據一個別的地方(比如在一個臨時表中),然後使用那些與你的CTE查詢結果。或者,您可以避免CTE查詢,併爲每個父級子級別分步執行此操作,並將其存儲在單獨的臨時表中並使用結果。希望有所幫助。

+0

順便說一句,在這裏找到類似的東西:http://stackoverflow.com/questions/9126704/why-cannot-we-use-outer-joins-in-recursive-cte/9126833#9126833 你可以嘗試更換左加入外部應用。 –