0

CTEに従って実行しましたが、正常に機能しますが、最後のノードの合計を取得する必要があるため、T1.Debit列を追加して計算すると、無効な列名「Debit」が返されます!!! オンラインで***

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)
)

混乱している私のコードの何が問題なのか教えてください!!!

実際には、借方、貸方の最後のノードの合計は返されません... !!! 次の写真を確認してください

ここに画像の説明を入力

4

2 に答える 2

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 )
    )
于 2013-09-23T08:47:42.180 に答える
0

Accounting.Topics列がないと思いますDebit

UPDTAE

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内部結合Accounting.Topics AS T1 ON T1.TopicID = c.NewParentID

上記のコードでは、T1.Debitを参照してAccounting.Topicsいます。

于 2013-09-23T07:36:13.607 に答える