我有一个用户(X)已经发送了15封电子邮件。有一个名为Opened的字段,取决于是否打开电子邮件,取决于1还是0。我想在列表中按下(按日期排序)并在每次达到零时开始计数,即每次收到电子邮件时计数,直到他打开它为止。然后我想平均所有的计数。我尝试过分区和各种各样的东西,但没有任何作用。
很抱歉,如果我把它放在错误的地方。这是我的最终代码,如果有人想要做类似的事情。
将@UserCount声明为Int 将@counter声明为int 将@StartEnd声明为varchar(10)
SET @ counter = 1 SET @ UserCount = 1
--Step through the table by user
WHILE @UserCount < (SELECT COUNT(UserID) FROM #Stage2)
BEGIN
--UserProcessOrder is the row number - update the dummy parameter @StartEnd
SET @StartEnd = (SELECT TOP(1) StartEnd FROM #Stage2 WHERE UserProcessOrder=@UserCount)
--Update the table with the counter value
UPDATE #Stage2 SET MaxBeforeOpen=@counter where UserProcessOrder=@UserCount
--Add 1 to the counter so that every time we see START we add 1
SET @Counter = @counter+1
--If we are at the end of the group then re-set the counter
IF @StartEnd='END' SET @counter = 1
--Go to next record for that user
SET @UserCount=@UserCount+1
END
- 从#Stage2
中选择*SELECT AVG(MaxBeforeOpen)as AverageEmailsBeforeOpen 从 ( 选择 * 来自#Stage2 在哪里StartEnd ='END' )如x