为什么我不能在这段代码中按组分别运行?

时间:2016-05-07 01:05:25

标签: sql sum

我希望能够为所有问题运行总计的赞成票。时间戳是CreationDate,但它只在总变化时返回值。

Declare @UserId int;
Select @UserId = ##UserId:int##; 
With
    upvotedQuestions As (
        Select v.CreationDate,
               Sum(Count(1)*5) Over (Order By v.CreationDate) As Score
            From Posts p
                 Inner Join Votes v
                On v.PostId = p.id
            Where v.VoteTypeId = 2 -- is an upvote
              And p.PostTypeId = 1 -- is a question
              And p.OwnerUserId = @UserId
            Group By v.CreationDate
    )
Select distinct
            v.CreationDate,
            upvotedQuestions.Score As [Upvote Question]

    From Votes v
        Left Outer Join upvotedQuestions On upvotedQuestions.CreationDate =v.CreationDate
    Where v.CreationDate >= (
            Select CreationDate
                From Users
                Where id = @UserId
            )
    Order By v.CreationDate
;

0 个答案:

没有答案