SQL: last month total

Please help with writing a query to get aggregated OrderAmount for each SalesPerson for the last month (last 28-31 days). For example, if today is 09/13/2018, we need sum from 08/13/2018 to 09/12/2018.


Thank you in advance.
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.

Éric MoreauSenior .Net ConsultantCommented:
something like this:

select salesperson, sum(salesamount)
from tablesales
where date > dateadd(month, -1, getdate()
group by salesperson

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
(no points..)

I would just add a cutoff to Éric Moreau's query to exclude the current date. Here's Eric's query with the cutoff

select salesperson, sum(salesamount)
from tablesales
where date > dateadd(month, -1, getdate()
and      date < CONVERT(date, getDate())
group by salesperson
Éric MoreauSenior .Net ConsultantCommented:
good catch
Mark WillsTopic AdvisorCommented:
And dont forget the closing brackets.....
select salesperson, sum(salesamount)
from tablesales
where [date] >= CONVERT(date,dateadd(month, -1, getdate()))
and   [date] < CONVERT(date, getDate())
group by salesperson

Open in new window

Using getdate() as a substitution for your actual date parameter by way of example. Using convert(date,<parameter>) to avoid the time component of a datetime / datetime2
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
.NET Programming

From novice to tech pro — start learning today.