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'。 –