Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 计算SQL中不同行的值之间的差异_Sql Server 2008_Partition - Fatal编程技术网

Sql server 2008 计算SQL中不同行的值之间的差异

Sql server 2008 计算SQL中不同行的值之间的差异,sql-server-2008,partition,Sql Server 2008,Partition,我有一张这样的桌子: Month_Date Account Cash 1 2222 5000 2 2222 6000 3 2222 7000 4 2222 10000 2 1111 5000 3 1111 7000 4 1111 8000 Month_Date Account Cash diff 1

我有一张这样的桌子:

Month_Date Account Cash 
1          2222    5000   
2          2222    6000  
3          2222    7000 
4          2222    10000 
2          1111    5000   
3          1111    7000  
4          1111    8000  
Month_Date Account Cash diff
1          2222    5000   NA
2          2222    6000  1000/5000= 0.2
3          2222    7000  1000/6000= 0.16
4          2222    10000 3000/7000 = 0.42
2          1111    5000   NA
3          1111    7000  2000/5000= 0.4
4          1111    8000  1000/7000= 0.14
我想要的输出应该是这样的:

Month_Date Account Cash 
1          2222    5000   
2          2222    6000  
3          2222    7000 
4          2222    10000 
2          1111    5000   
3          1111    7000  
4          1111    8000  
Month_Date Account Cash diff
1          2222    5000   NA
2          2222    6000  1000/5000= 0.2
3          2222    7000  1000/6000= 0.16
4          2222    10000 3000/7000 = 0.42
2          1111    5000   NA
3          1111    7000  2000/5000= 0.4
4          1111    8000  1000/7000= 0.14
当然没有计算。 并非所有账户都有第一个月的数据,请参见账户1111,因此我想对它们进行排名。 我正在寻找类似于在帐户分区上进行减法和除法的行

Select Month_Date, Account, Cash, 
rank() over (Partition by Month_Date, Account) order by Month_Date as Rnk, 
Diff = 
case when Rnk = 1 then 'NA' else ????

如何执行此操作有何建议

适用于SQL Server 2012之前的版本

SELECT
    M.Month_Date,
    M.Account,
    M.Cash,
    Diff = X.Cash -  M.Cash
FROM
    MyTable M
    OUTER APPLY
    (SELECT TOP 1
        *
    FROM
        MyTable M2
    WHERE
        M.Account = M2.Account
        AND
        M.Month_Date < M2.Month_Date
    ORDER BY
        Month_Date
    ) X
对于SQL Server 2012+您可以使用滞后/超前函数。在SQL Server 2008中,可以使用外部应用运算符确定上一行:

DECLARE @t TABLE
    (
      Month_Date INT ,
      Account INT ,
      Cash INT
    )
INSERT  INTO @t
VALUES  ( 1, 2222, 5000 ),
        ( 2, 2222, 6000 ),
        ( 3, 2222, 7000 ),
        ( 4, 2222, 10000 ),
        ( 2, 1111, 5000 ),
        ( 3, 1111, 7000 ),
        ( 4, 1111, 8000 )

SELECT t.*, ROUND((t.Cash - oa.Cash) * 1.0 / oa.Cash, 2) AS Diff
FROM @t t
OUTER APPLY(SELECT TOP 1 * FROM @t 
            WHERE Account = t.Account AND 
                  Month_Date < t.Month_Date 
            ORDER BY Month_Date DESC) oa

3000/10000的原因是什么?对不起,输入错误。