SQLTeam.com | Weblogs | Forums

How to get current quarter and prior quarter values


#1

Hi Members,
Hope all is well. I am trying to build SQL query which pulls order value of a customer and the order value of same quarter but of prior year from the same customer. Can someone please explain the SQL format with an example I should follow?

For example- There are three tables i.e. A(customer data), B(order quantity), C (order amount) Each table has a common key 'keycutomer'-serial number of each customer.

Now I want to pull what is the order value and quantity of Mr. A in 4Q2017 and 4Q2016.


#2

If you want to pull for quarters, you're gonna a date column somewhere :grin:


#3

yeah...let's assume all other required columns are there in tables.


#4

Please post consumable sample data


#5

Not sure if this is what your looking for?

To get the first day of the current quarter:
SELECT DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)

To get the last day of the current quarter:
SELECT DATEADD (dd, -1, DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) +1, 0))

To get the first day of the previous quarter
SELECT DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, 0)