Link to home
Start Free TrialLog in
Avatar of Alex A
Alex A

asked on

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.

TableOrders
----------------
SalesPerson
OrderAmount
OrderDate


Thank you in advance.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start 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
good catch
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial