count number of orders in 6 month intervals

I am trying to create a query\procedure that will show the total number of orders from a selected customer in 6 month intervals. For 6, 12, 18 and 24 months. Please show a step by step.

example if abc company order 6 times from Jan to Jun and 4 times from Jul-Dec then i want the report to show

company    6_Months    12_Months
acbc                   6                  10 <----- Rolling sum

I am using microsoft sql server 14
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Kyle AbrahamsSenior .Net DeveloperCommented:
declare @date = DATEADD(d,0,DATEDIFF(d,0,GETDATE()))   -- today at midnight)

select company, 
-- if order is >= 6 months prior to today, add 1, else don't add anything end
sum (case when order_date >= dateadd(m, -6, @date) then 1 else 0 end) 6_Months,
sum (case when order_date >= dateadd(m, -12, @date) then 1 else 0 end) 12_Months
where company = @company
group by company

Open in new window


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
cskehanCFOAuthor Commented:
Had to do a little tweaking to code, but you got me in the right directions. Thanks!!
Olaf DoschkeSoftware DeveloperCommented:
You might also look into my solution of a similar problem at

Bye, Olaf
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.