sql - Using a dummy variable to keep a count -


i have user (x) has been sent 15 emails. there field called opened 1 or 0 depending if opened emails. want work down list (ordered date) , start count each time 0 i.e. count each time gets email until opens it. want average out counts. have tried partitioning , various things nothing works.

sorry if putting in wrong place. final code, should want similar.

declare @usercount int declare @counter int declare @startend varchar(10)

set @counter=1 set @usercount=1

--step through table user while @usercount < (select count(userid) #stage2) begin     --userprocessorder row number - update dummy parameter @startend     set @startend = (select top(1) startend #stage2 userprocessorder=@usercount)      --update table counter value     update #stage2 set maxbeforeopen=@counter userprocessorder=@usercount      --add 1 counter every time see start add 1     set @counter = @counter+1      --if @ end of group re-set counter     if @startend='end'  set @counter = 1      --go next record user     set @usercount=@usercount+1 end 

--select * #stage2

select avg(maxbeforeopen) averageemailsbeforeopen ( select * #stage2 startend='end' ) x


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -