簡體   English   中英

SQL父子查詢不起作用

[英]Sql parent Child Query not working

我想獲取父母及其子女的數據,意味着首先要成為父母,然后是他們的子女,以下是我要轉換為父母及其子女的查詢

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'.

然后我更改查詢並刪除co​​unt(*)

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'.

實際上,這是設計使然 ,請閱讀《定義和使用遞歸公用表表達式的准則》。

遞歸成員的CTE_query_definition中不允許以下各項:

  • 選擇地區
  • 通過...分組
  • HAVING
  • 標量聚集
  • 最佳
  • 左,右,外聯接(允許內聯接)
  • 子查詢

您必須將LEFT JOIN和COUNT移出CTE查詢,將這些數據存儲在其他位置(例如在臨時表中),然后再將它們與CTE查詢的結果一起使用。 或者,您可以避免CTE查詢,並針對每個級別的父子對象分別進行此步驟,將其存儲在單獨的臨時表中並使用結果。 希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM