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.
TableOrders
----------------
SalesPerson
OrderAmount
OrderDate
Thank you in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
good catch
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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