在计算SQL中两行之间的差异时重置计数

时间:2016-09-07 19:47:32

标签: sql count rows reset difference

这是我目前拥有的代码。它计算评论业力中的第一个值与下一个值之间的差异。链接因果相同的事情。我需要计数在显示新作者时重新启动。这是我无法弄清楚的部分。

SELECT [author]
    , [Date (Toronto)]
    , Subreddit
    , Likes
    , Moderator
    , [User: is_gold]
    , [User: link karma]
    , [User: comment karma]
    , COUNT(type) AS #comment
    , coalesce([User: comment karma] - LAG([User: comment karma]) OVER (
            ORDER BY author
                , [Date (Toronto)]
            ), 0) AS diff_comment_karma
    , coalesce([User: link karma] - LAG([User: link karma]) OVER (
            ORDER BY author
                , [Date (Toronto)]
            ), 0) AS diff_link_karma
FROM Comment
GROUP BY Author
    , [Date (Toronto)]
    , Subreddit
    , Likes
    , Moderator
    , [User: is_gold]
    , [User: link karma]
    , [User: comment karma]
    , Type
ORDER BY Author
    , [Date (Toronto)]

这是我目前的结果

author      User: comment karma    User: link karma    diff_comment_karma   diff_link_karma
CheezGrater 20665                   7840               19394                7530
CheezGrater 20665                   7840               0                    0
CheeziTater 1                       1                 -20664               -7839
CheeziTater 1                       1                  0                    0
Cheezsteak7 45                      174                44                   173
CheezulWeezul   1                   1                 -44                  -173
CheezulWeezul   1                   1                  0                    0
chef_baboon 4266                    1643               4265                 1642
chef_baboon 4266                    1643               0                    0
chef_baboon 4266                    1643               0                    0

这是我想要的结果

author      User: comment karma    User: link karma    diff_comment_karma   diff_link_karma
CheezGrater 20665                   7840               19394                7530
CheezGrater 20665                   7840               0                    0
CheeziTater 1                       1                  **0                  0**
CheeziTater 1                       1                  0                    0
Cheezsteak  45                      174                **0                    0**
CheezulWeezul   1                   1                  **0                  0**
CheezulWeezul   1                   1                  0                    0
chef_baboon 4266                    1643               **0                  0**
chef_baboon 4266                    1643               0                    0
chef_baboon 4266                    1643               0                    0

0 个答案:

没有答案