如何在sql 2000中找到不同用户的连续行的差异?

时间:2014-12-04 17:34:23

标签: sql sql-server-2000

我试图通过用户找到连续行中数字的差异。

update [current]
set diff= 
   ISNULL([next].number, 0) - [current].number
FROM
   my_table       AS [current]
LEFT JOIN
   my_table       AS [next]
      ON [next].ID = (SELECT MIN(ID) FROM my_table WHERE ID > [current].ID)
where [current].Date = [next].Date
and [current].User = [next].User

此代码不允许我区分用户。 它只是为所有用户找到连续行的差异。 我希望它能找到同一用户的连续行的差异。

请帮忙。

1 个答案:

答案 0 :(得分:1)

如果您只想获取用户的下一个号码,请将您的WHERE条件移动到ID匹配的JOIN中:

SELECT 
    *
FROM
    my_table AS [current]
    LEFT JOIN my_table AS [next]
        ON [next].ID = (
            SELECT 
                MIN(ID) -- get first ID
            FROM
                my_table 
            WHERE
                ID > [current].ID  -- greater than the current
                and my_table.[User] = [current].[User] -- same user
                and my_table.[Date] = [current].[Date] -- same day
        )