How to get the sales for last quarter and last month when we are in january in SQL Server?

how to get previous month data from current date in sql
how to get previous month data from current date in sql server
get month start date and end date in sql
how to get monthly data in sql server
sql date minus 1 month
how to get next date in sql

For example, we have the data of the years 2017, 2018 & 2019. In case I am in January 2019, I want the data of the last quarter and last month.

My query is like this:

        When month(datecolumn) = month(getdate()) - 1 
           then sales 
           else 0 
     end) as premonthsales,
        when datepart(qq, datecolumn) = datepart(qq, getdate()) - 1 
           then sales 
           else 0 
     end) as lq sales 

When I am in January 2019, it is showing me the data of both December 2017 and 2018. I want it dynamic like when I am in Jan 2018, it should show only data of Dec 2017 for last month and last quarter (4rth quarter of 2017).

If you're just after the previous quarter, just use basic date logic. This is Pseudo-SQL, however:

FROM ...

For Jan '19, Dec '18 will already be in the previous quarter, so you don't need additional logic.

SQL Tricks, These statements are tested with T-SQL under MS SQL Server. SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) + 1, -1) -- Last day of this� previous-previous quarter is 1 and 1st quarter last month is 3. If the date is 2020-03-14, output month:9 year:2019 previous-previous quarter is 3 and 3rd quarter last month is 9. Did this for Month: declare @month int; declare @quarter int set @quarter= DATEPART(Quarter, '2020-10-14') set @month = (@quarter - 2) *3 select @month select @quarter

To get the first day of the previous quarter in SQL Server, use the following code:


To get the last day of the previous quarter:


To get the first day of the current quarter:


You get all the dates, based on obtained date apply filter on your data to get the correct result.

FROM ...
WHERE DateColumn between start_date and end_date;


SQL date format Overview; DateDiff SQL function, DateAdd SQL , For professionals just getting started with SQL Server, these In the query, we're passing integer value that consists of year, 2008; month, Query the HireDate column to return only the Quarter portion of the date FROM sales. the number of years between the first hire date and the last hire date. I would like to get the dates for the last quarter and next quarter. tagged sql sql-server-2008 tsql or ask previous quarters month and year in SQL Server.

You should include YEAR as well

      (CASE WHEN MONTH(datecolumn) = MONTH (GETDATE()) - 1 AND  YEAR(datecolumn) = YEAR(GETDATE()) - 1 THEN sales ELSE 0 END) AS premonthsales ,
      (CASE WHEN DATEPART(qq,datecolumn) = DATEPART(qq,GETDATE()) - 1 AND AND  YEAR(datecolumn) = YEAR(GETDATE()) - 1 THEN Sales ELSE 0 END) AS lq sales 

Display Last N Months & Selected Month using Single Date , Below, you can see that I have displayed 3 regular measures based on the selected month (Sales for selected month, Sales YTD and Sales Last Year) along � To get the first day of the previous quarter in SQL Server, use the following code:. SELECT DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, 0) To get the last day of the previous quarter:

Simplify Date Period Calculations in SQL Server, In this tip we look at different SQL Server date functions to assist with getting beginning and ending periods such as first day of month, first day of quarter, etc. If you use traditional calendar quarters (Jan-Mar is Q1, etc.) that queries the sys.databases table to find databases created in the last quarter. You� Find below SAP HANA SQL Script to get Current Quarter Beginning Date, Last Quarter Beginning Date, and Next Quarter Beginning Date. Suppose you have business requirements based on some date column you need to do some manipulation or calculation based on Current Quarter Beginning Date, Last Quarter Beginning Date, or Next Quarter Beginning Date.

How To Find Employees Hired Last Month Or Year In SQL Server, In this blog, we will understand how we can find employees hired last month or year. We will understand DATEDIFF function in SQL server. Farhan Ahmed; Updated date Jan 15, 2019 insert into Employees values('Cara Stevens','Male', 30,'Pre-Sales Support','New York','2018-11-01',162700); insert into Employees� You can create Quarter column first of all. Quarter= ROUNDUP(MONTH(Table1[Date])/3,0) Then create measures to get the current quarter total sales and last quarter total sales. Total sales_current quarter= CALCULATE(SUM(Table1[sale]),FILTER(ALLSELECTED(Table1), Table1[Quarter] =MAX(Table1[Quarter])))

How to Handle Calculations Related to fiscal year and quarter , To get the right data based on this new “year” and “quarter”, we need to redefine at a different date instead of January 1, we need to add the difference in. the year starts two month earlier, we need to slightly modify the code to the year), there is no difference than the previous query, but if we run this� We can get the month from a date using MONTH () formula. If we divide the month by 3 and then round the value up to nearest integer we will get the Quarter. So, A formula like =ROUNDUP (MONTH (B4)/3,0) should tell us the quarter for the month in the cell B4.

  • "In case I am in january 2019 I want the data of last quarter and last month" So you want the data for October to December 2018? December 2018 is already in the last quarter; so saying you want that in addition seems pointless when it's already included. Would be mean, in March '19, you want want Oct18-Dec18 and Feb 19?
  • @Larnu I mean to say the filter can be dynamic it might be jan 2019 or march 2019 but I want to calculate sales for it's previous month and previous quarter no matter how many years data are available.
  • This is not dynamic #Never because if I am in april of 2019 then it will produce wrong results.