来自事务SQL的帐户报表

时间:2016-04-25 20:50:21

标签: sql sql-server sql-server-2005

我有一个表的事务列表,用于记录UserID,DateTime,TransactionType和Value的用户。 TransactionID是主键自动增量1. TransactionType定义Deposit(1)或Withdrawal(2),因此表中的所有值都是正数。我正在尝试使用总计创建一个帐户报表。

TransactionID    UserID    DateTime           TransactionTypeID    Value
     1            3112     01-04-2016 12:00          1              5.00
     3            3112     01-04-2016 13:00          2              2.00
     5            3112     01-04-2016 13:25          2              1.00
     8            3112     02-04-2016 12:00          1              10.00
     9            3112     02-04-2016 12:35          2              4.00

基本上我想创建一个运行的帐户查询语句,其中包含要创建的总值:

DateTime           TransactionTypeID    Deposit    Withdrawal    Balance
01-04-2016 12:00          1              5.00                     5.00
01-04-2016 13:00          2                          2.00         3.00
01-04-2016 13:25          2                          1.00         2.00
02-04-2016 12:00          1              10.00                   12.00
02-04-2016 12:35          2                          4.00         8.00

我尝试使用OUTER APPLY来选择上一个事务,但在单个查询中没有优先权。任何帮助将不胜感激

SELECT 
    [UserID], [DateTime], 
    T.[Value] * 
    (CASE 
        WHEN [TransactionTypeID] IN (1, -- deposit
                                     2 -- withdrawal
                                    )
            THEN -1
            ELSE 1
     END),
    T2.Value AS PrevValue
FROM 
    [Transaction] T
OUTER APPLY 
    (SELECT TOP 1 T2.[Value]
     FROM [Transaction] T2
     WHERE UserID = 3112 
       AND T2.[TransactionID] > T.TransactionID
    ORDER BY T2.TransactionID) AS T2
WHERE 
    T.[UserID] = 3112
ORDER BY 
    T.[TransactionID] DESC

2 个答案:

答案 0 :(得分:0)

您要求的查询应该看起来像这样..

SQL 2005

SELECT  [DateTime],
        [TransactionID],
        [Deposit]    = CASE WHEN TransactionTypeID = 1 THEN [Value] END,
        [Withdrawal] = CASE WHEN TransactionTypeID = 2 THEN -[Value] END,
        [Balance]    = CASE TransactionTypeID WHEN 1 THEN [Value] 
                                              WHEN 2 THEN -[Value] END + ISNULL([PrevValue], 0)
FROM    [Transaction] t1
        OUTER APPLY (SELECT SUM(CASE TransactionTypeID WHEN 1 THEN [Value] 
                                                       WHEN 2 THEN -[Value] END) AS [PrevValue]
                        FROM [Transaction] t2 
                        WHERE t1.UserID = t2.UserID AND t2.[DateTime] < t1.[DateTime]) ca  
WHERE   UserID = 3112

SQL 2008 +

SELECT  [DateTime],
        [TransactionID],
        [Deposit]    = CASE WHEN TransactionTypeID = 1 THEN [Value] END,
        [Withdrawal] = CASE WHEN TransactionTypeID = 2 THEN -[Value] END,
        [Balance]    = SUM(CASE TransactionTypeID WHEN 1 THEN [Value] 
                                                  WHEN 2 THEN -[Value] END) 
                            OVER (ORDER BY [DateTime])
FROM    [Transaction]
WHERE   UserID = 3112

答案 1 :(得分:-1)

我找到了一个SQL Server 2012解决方案。如果您运行的是旧版本,请检查备选方案。

它使用SUM OVER ..

select [TransactionID],
[DateTime],
( CASE WHEN [TransactionTypeID] IN (
                                1, -- deposits
                                2 -- withdrawals
                              )
                     THEN -[Value]
                     ELSE [Value]
                     END


                ),
       sum(( CASE WHEN [TransactionTypeID] IN (
                                1, -- deposits
                                2 -- withdrawals
                              )
                     THEN -[Value]
                     ELSE [Value]
                     END


                )) over(order by [DateTime] rows unbounded preceding) as runningtotal
from [Transaction]
WHERE UserID = 3112