Assuming that none of my votes are for questions, how many days until I get the Electorate badge.
プログラマーとプログラミングに熱心な人々のための質問と回答
-- Worst Case Scenario - How long until the Electorate Badge -- Assuming that none of my votes are for questions, how many days -- until I get the Electorate badge. declare @UserId int = ##UserId## declare @voteCount int select @voteCount = u.UpVotes + u.DownVotes from Users u where u.Id = @UserId -- using equivalent of max given by this answer: http://stackoverflow.com/questions/124417/is-there-a-max-function-in-sql-server-that-takes-two-values-like-math-max-in-net/293804#293804 select case when @voteCount < 600 then ceiling(0.5 * ((600 - @voteCount)/30.0 + 5) + ((600 - @voteCount)/30.0 - 5)) when exists(select 1 from Badges where UserId = @UserId and Name = 'Electorate') then 0 else ceiling(@voteCount / (4 * 30.0)) end as ElectorateState