SQL Query Monthly growth percentage increase/decrease

Hi Guys,

I have stumbled across the below query from the internet and would like to know how I can apply it to my SQL query. I am currently using SQL Server 2012.

    month, total,
    (total::float / lag(total) over (order by month) - 1) * 100 growth
from (
    select to_char(created_at, 'yyyy-mm') as month, count(user_id) total
    from users
    group by month
) s
order by month;

Open in new window

The link to this code is http://stackoverflow.com/questions/16357876/sql-query-growth-of-users-per-month-in-percentage however it is for Postgres.

Could someone please help me translate this for MS SQL. Please note that I would need to SUM my totals for a month as an account could have multiple transactions within a single month.

Many thanks
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

databarracksAuthor Commented:
LEAD() & LAG() functions exist in SQL Server 2012

to_char(created_at, 'yyyy-mm') is a method of getting month and year from date information, this isn't hard in TSQL basically get the 1st of a month, it gives you the same effect.
    , total
    , (total * 1.0 / LAG(total) OVER (ORDER BY yrZ_month) - 1) * 100 growth
                  DATEADD(dd, -(DAY(created_at) - 1), created_at) AS yr_month
                , COUNT(user_id)                                  AS total
            FROM users
            GROUP BY
      ) s

Open in new window

I'm not certain that minus one belongs here, and it may be necessary to multiply that lag() value by 1.0 to get a fractional result.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
Are you working with SQL Server sysusers view? Because if you do, won't work. That view don't returns deleted users.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

databarracksAuthor Commented:
Hi Guys,

PortletPaul your query seems to be working correctly:) However I am interested to know I bit more about "multiply that lag() value by 1.0" and why you are suggesting that this may be a problem?

Other than that I think we are onto a winner.
I didn't do any testing I was just "translating" using "the little grey cells"

re:"multiply that lag() value by 1.0"
Normally I multiply the divisor by 1.0  e.g. ([someintvalue] / [someotherinteger] * 1.0) as something
So I wasn't sure if integer math would take over if that wasn't done. If you are getting good results that's fine.

I'm still not sure why that minus one exists though.
databarracksAuthor Commented:
Ok great thanks for your help PortletPaul I am happy with the results
databarracksAuthor Commented:
PortletPaul is always on the ball :)
thanks, is that a jingle?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.