简体   繁体   English

SQL父子查询不起作用

[英]Sql parent Child Query not working

I want to get parent and their child data,means first come parent then their childs,Below is query that i want to convert to parent and their child 我想获取父母及其子女的数据,意味着首先要成为父母,然后是他们的子女,以下是我要转换为父母及其子女的查询

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'

I try below query 我在下面查询

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

after executing these query i got this error 执行这些查询后,我得到了这个错误

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

Then i change my query and remove count(*) 然后我更改查询并删除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 

then i got this error 然后我得到这个错误

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

Actually it is by design, read Guidelines for Defining and Using Recursive Common Table Expressions 实际上,这是设计使然 ,请阅读《定义和使用递归公用表表达式的准则》。

The following items are not allowed in the CTE_query_definition of a recursive member: 递归成员的CTE_query_definition中不允许以下各项:

  • SELECT DISTINCT 选择地区
  • GROUP BY 通过...分组
  • HAVING HAVING
  • Scalar aggregation 标量聚集
  • TOP 最佳
  • LEFT, RIGHT, OUTER JOIN (INNER JOIN is allowed) 左,右,外联接(允许内联接)
  • Subqueries 子查询

You have to move your LEFT JOINs and COUNT out of the CTE query, store those data somewhere else (like in a temp table) and then later use those with the result of your CTE query. 您必须将LEFT JOIN和COUNT移出CTE查询,将这些数据存储在其他位置(例如在临时表中),然后再将它们与CTE查询的结果一起使用。 Or you can avoid CTE query and do this step by step separately for each level of parent-child, store in separate temp tables and use the results. 或者,您可以避免CTE查询,并针对每个级别的父子对象分别进行此步骤,将其存储在单独的临时表中并使用结果。 Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM