这有帮助吗?- 请注意,如果您可以发布您的表格定义,它会有所帮助
DECLARE @transactions TABLE
(
    [AccountNumber] INT NOT NULL,
    [TransactionAmount] DECIMAL(18, 10) NOT NULL,
    [KindOfTransaction] CHAR(1) NOT NULL, -- this could be better served as a boolean / bit field
    [TransactionFile] INT NOT NULL
)
-- dummy data
INSERT INTO @transactions VALUES(4568643, 100, 'C', 123)
INSERT INTO @transactions VALUES(4568643, 150, 'C', 124)
INSERT INTO @transactions VALUES(4568643, 50, 'D', 125)
INSERT INTO @transactions VALUES(2345623, 100, 'C', 126)
INSERT INTO @transactions VALUES(2345623, 79, 'C', 127)
    -- here's the actual query to return your data using a common table expression
;WITH cteAccountSummary (AccountNumber, TotalCredits, TotalDebits) AS (
    SELECT  [AccountNumber], 
            SUM(
                CASE [KindOfTransaction]
                    WHEN 'C' THEN [TransactionAmount]
                    ELSE 0
                END
            ),
            SUM(
                CASE [KindOfTransaction]
                    WHEN 'D' THEN [TransactionAmount]
                    ELSE 0
                END
            )
    FROM @transactions
    GROUP BY [AccountNumber]
)
SELECT [AccountNumber], TotalCredits - TotalDebits AS 'NetFinancialPosition'
FROM cteAccountSummary