0

我已经完成了下面的CTE它工作正常,但我需要得到最后一个节点的总和,所以问题是当我添加T1.Debit列计算其给我一个无效的列名称'借记'!在线***Sql递归错误无效列名CTE

ALTER Function [dbo].[SubTopics_GetSum] 
-- Add the parameters for the stored procedure here 
    (
    @TopicID int 
    ) 
RETURNS TABLE 
AS 
RETURN 
(
    -- Add the SELECT statement with parameter references here 
    WITH cte 
    AS (
     SELECT 
      T1.TopicID, 
      T1.Code, 
      T1.Description, 
      T1.ParentID, 
      T1.ParentID AS NewParentID, 
      CAST(T1.Code AS nvarchar(MAX)) AS TopicCode, 
      CAST(T1.Description AS nvarchar(MAX)) AS TopicDescription, 
      isnull((Accounting.DocumentDetail.Debit),0) AS Debit, 
      isnull((Accounting.DocumentDetail.Credit),0) AS Credit 
     FROM Accounting.Topics AS T1 
      LEFT OUTER JOIN Accounting.DocumentDetail 
       ON T1.TopicID = Accounting.DocumentDetail.TopicFK 
     where NOT EXISTS(
         SELECT 
          T2.TopicID, 
          T2.Code, 
          T2.Description, 
          T2.ParentID, 
          isnull((Accounting.DocumentDetail.Debit),0) AS Debit, 
          isnull((Accounting.DocumentDetail.Credit),0) AS Credit 
         FROM Accounting.Topics AS T2 
          LEFT OUTER JOIN Accounting.DocumentDetail 
           ON T2.TopicID = Accounting.DocumentDetail.TopicFK 
         WHERE (ParentID = T1.TopicID) 
         ) 
     UNION ALL 
     SELECT 
      c.TopicID, 
      c.Code, 
      c.Description, 
      c.ParentID, 
      T1.ParentID AS NewParentID, 
      CAST(T1.Code AS nvarchar(MAX)) + c.TopicCode AS TopicCode, 
      CAST(T1.Description AS nvarchar(MAX)) + ' - ' + c.TopicDescription AS TopicDescription, 
      *** isnull((T1.Debit),0)+isnull(c.Debit,0) AS Debit,--IN THIS LINE error 'Invalid Column Name 'Debit'' 
      isnull(c.Credit,0) AS Credit 
     FROM cte AS c 
      INNER JOIN Accounting.Topics AS T1 
       ON T1.TopicID = c.NewParentID 
     ) 

    SELECT isnull(sum(Debit),0)AS Debit, 
     isnull(sum(Credit),0)AS Credit 
    FROM cte AS c 
    WHERE (NewParentID = @TopicID) 
) 

让我知道什么是错的,我的代码混淆了!!!

实际上它不会返回我最后一个节点的借方,贷方...总和! 检查以下PIC

enter image description here

+0

对于帮助我们找到可以很好的格式化代码行。 –

回答

1

我想下面的代码会有所帮助,

ALTER FUNCTION [dbo].[Subtopics_getsum] 
-- Add the parameters for the stored procedure here 
(@TopicID INT) 
returns TABLE 
AS 
    RETURN ( 
     -- Add the SELECT statement with parameter references here 
     WITH cte 
      AS (SELECT T1.topicid, 
         T1.code, 
         T1.description, 
         T1.parentid, 
         T1.parentid          AS 
         NewParentID 
         , 
         Cast(T1.code AS NVARCHAR(max)) 
         AS TopicCode, 
         Cast(T1.description AS NVARCHAR(max))   AS 
         TopicDescription, 
         Isnull((accounting.documentdetail.debit), 0) AS Debit, 
         Isnull((accounting.documentdetail.credit), 0) AS Credit 
       FROM accounting.topics AS T1 
         LEFT OUTER JOIN accounting.documentdetail 
            ON T1.topicid = 
             accounting.documentdetail.topicfk 
       WHERE NOT EXISTS(SELECT T2.topicid, 
             T2.code, 
             T2.description, 
             T2.parentid, 
    Isnull((accounting.documentdetail.debit), 0) 
    AS 
    Debit, 
    Isnull((accounting.documentdetail.credit), 0) AS 
    Credit 
    FROM accounting.topics AS T2 
    LEFT OUTER JOIN accounting.documentdetail 
    ON T2.topicid = 
    accounting.documentdetail.topicfk 
    WHERE (parentid = T1.topicid))) 
    SELECT Isnull(Sum(debit), 0) AS Debit, 
    Isnull(Sum(credit), 0)AS Credit 
    FROM cte AS c 
    WHERE (newparentid = @TopicID) 
    ) 
+0

仍然不会返回最后一个节点总数! –

+0

你能用例子来解释想要的输出吗? –

+0

我有一个表中有父子行(存储会计主题),它与存储会计行的其他表“documentdetail”相关NOW 我需要计算每个主题余额(借方,贷方...)在第一篇文章中附加的图片,但如果你看图片,我不能得到所选的节点,这是最后一个的借方,信贷的总和,你可以看到我的代码返回0为最后一个节点,但在选定的行它必须返回信用= 5000000,借记= 0 –

0

我想你Accounting.Topics还没有列Debit

UPDTAE

ISNULL((T1.Debit),0)+ ISNULL(c.Debit,0)AS借记, - 在此行错误 '无效的列名称借记'' ISNULL(c.Credit,0)AS信用 FROM CTE为C INNER JOIN Accounting.Topics AS T1 ON T1.TopicID = c.NewParentID

在上面的代码你T1.DebitAccounting.Topics

+0

Accounting.Topics没有,但在这里我称T1.Debit这个列是在顶行中定义的! –

+0

那么如何引用'accounting.documentdetail.debit'而不是't1.debit'? –

+0

你已经将它引用为'c.Debit'。 –