How to calculate Last 2 months average sales in sql server

Adimulam Yaswanth
Adimulam Yaswanth used Ask the Experts™
on
How to calculate Last 2 months average sales in sql server (excluding the current month)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
SQL Server DBA & Architect, EE Solution Guide
Awarded 2009
Distinguished Expert 2018
Commented:
Without knowing your table structure or query, you can start like this.. Pls make sure you change the column names and table names before running the query..
select avg(sales)
from ur_table
WHERE datetime_col >= dateadd(m,-2,dateadd(m, datediff(m, 0, getdate()), 0)) and datetime_col < dateadd(m, datediff(m, 0, getdate()), 0)

Open in new window

Author

Commented:
Thankyou Raja Jegan R sir...

I tried in CTE but u gave me optimised solution using select statement.. Thankyou sir..
Raja Jegan RSQL Server DBA & Architect, EE Solution Guide
Awarded 2009
Distinguished Expert 2018

Commented:
Welcome, glad to assist..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial