SQL Query Last Three Full Months (Excluding Current Month) - sql

I have a database with millions of rows and many years of data. I'm currently using PowerBI to query the data (ODBC connection), but I don't want to pull everything. (Recent convert from Crystal.) Instead, I'm trying to write a clause into my WHERE statement to only query the last three full months of data (excluding the current month.) So, today being Jan 25,2023 I want to capture Oct 1 2022 - Dec 31 2022. I would then schedule this so it always pulls the last three months.
My dataset does contain a field that stores the date so I'm trying to write something like the below that I'm able to use to pull previous days.
"Table"."OrderDate" >= DATEADD(d,DATEDIFF(d,0,CURRENT_TIMESTAMP)-1,0) AND
"Table"."OrderDate" < DATEADD(d,DATEDIFF(d,0,CURRENT_TIMESTAMP),0))
Everything so far that I've tried (and found online) gives odd results. Pulls Nov - Jan and the like.
Thank you for any help you can provide.

You can use the following query to pull the last three full months of data. This query uses the DATEADD and DATEDIFF functions to calculate the date range for the last three full months.
WHERE "Table"."OrderDate" >= DATEADD(month, DATEDIFF(month, 0, CURRENT_TIMESTAMP) - 3, 0) AND "Table"."OrderDate" < DATEADD(month, DATEDIFF(month, 0, CURRENT_TIMESTAMP), 0)

Related

DATEADD to return data in the last 3 months but I want full month of data

So my query aims to grab all data in the last 3 months, but only returns data when it is a full month.
I have tried:
WHERE Created_Date > DATEADD(MONTH, -3, GETDATE())
or
WHERE Created_Date > DATEADD(DAY, -90, GETDATE())
Both ways return the data in the last 3 months starting from current date. But the thing is, since my query wants to get aggregated data, so if today is 8th Aug, 3 months dating back means May has not got the full month of data (from 1st to 31st), so the aggregated data is not fully reported in the results. Does this make sense?
Is there any other way to return the full month data?
I know that we can use #startOfCurrentMonth like in here but this is 3 months we are aiming to get.
To get the days of the current month plus three full months back, simply subtract four months and get that month's last day. Then take any dates after that day.
WHERE created_date > EOMONTH(DATEADD(MONTH, -4, GETDATE()))
If created_date is a misnomer and contains datetimes instead of dates, add a day and include that:
WHERE created_date >= DATEADD(DAY, 1, EOMONTH(GETDATE(), -4)))

SQL connection within Excel- date range

I need to be able to look at the previous 3 months of data using a SQL statement within Excel.
I have the below code which I've been able to build but it doesn't do what I need 100%.
It gives me the last 3 months and the last date of the 4th month and the first date of the current month.
I could use Excel VBA and delete out data that is the first of current and last of the month 4 months ago.
However, I was wondering if anyone could help correct the below.
I've tried using EOMNTH and this brings an error up and I cant seem to use a DECLARE statement either.
If the below could be changed to only bring back information from the last 3 months (Jan, Feb and March) that would be great.
If someone could explain how I could change the below for only dates within the previous month that would be helpful too.
where T1."Receive_Date" between DATEADD(dd, -DAY(DATEADD(mm, -4, getdate())), DATEADD(mm, -3, getdate()))
and DATEADD(dd, -DAY(getdate()) + 1, getdate())

SQL how to get information from The Start of some # of years ago

I have looked through a few posts but either there is too much information or I just don't quite understand what I am looking for.
eg: How to get the first and last date of the current year?
In a table I have sales orders for the last 20 years, all I want is to show the orders from January 1st of three years before until whatever the current date is.
AND ShipDate >= (YEAR(ShipDate)-3)
Which doesn't work.
AND ShipDate >= DATEADD(YEAR,-2,GETDATE())
Which Shows exactly 2 years ago from whatever the current day is.
I want to be able to eventually create reports that show each year in the last three on their own but this is the first step and I am not doing well so far!
I want is to show the orders from January 1st of three years before until whatever the current date is.
The best method in SQL Server is:
where shipdate >= datefromparts(year(getdate()) - 3, 1, 1)
For any date in 2021, this returns all rows from 2021, 2020, 2019, and 2018. If you don't want 2018, then use - 2 rather than - 3.
This formulation has the option of being optimizer friendly (allowing the use of indexes and partitions, for example). Two other common says to write this are not optimizer friendly:
where datediff(year, shipdate, getdate()) <= 3
where year(shipdate) >= year(getdate()) - 3
(Once again, the "3" might be "2" depending on what you really intend.)
you were close:
AND YEAR(ShipDate) >= (YEAR(ShipDate)-3)
You were comparing a year to a date

Report that updates yearly

I have created a report that is supposed to look at the number of baptisms at our church for the ministry year. The Ministry year runs from Aug 1 - July 31. I currently have the report set to tell me the names of anyone that has a baptism date greater than 8/1/2016. But I would need to change that year each year for it to report properly. so I wanted to use a Case statement to have it update each year, but i am getting an error message with this: (The error is in the where clause, so I didn't include the entire report)
WHERE (P.organization_id = 1) AND
((CandidateProcesses_BaptismDate68.datetime_value) between (
case
When datepart(month, getdate()) < 8 then ('8/1/'+ datepart(year, getdate()))
When datepart(month, getdate()) >7 then ('8/1/'+
datepart((year,getdate())-1))End) and Getdate())
Does anyone see why I am getting an error?
Thanks!
You are getting an error trying to add a string and a number. You could fix that using datename() rather than datepart(). But, I think this is a simpler approach:
WHERE (P.organization_id = 1) AND
year(dateadd(month, -7, CandidateProcesses_BaptismDate68.datetime_value)) = year(dateadd(month, -7, getdate()))
This subtract 7 months to get the "ministry year" and then compares that to the current date minus seven months. That is, it subtracts 7 months and then normalizes on the calendar year.
This is a bit more expensive than your version, because it cannot use an index on CandidateProcesses_BaptismDate68(datetime_value). However, I doubt the database of baptisms is so large that the query will take very long anyway. (If that is an issue, then your version can be made to work with some simple modifications.)

SQL Server - Quarterly data Report

I have recently wrote a query (stack overflow helped) to generate annual data from a database.
the logic used is : any day in this year will always give all the results from last year.
where year (table.datecolumn) = year(GETDATE())-1
Now, I have been asked to do a similar thing with quarterly data and I am having a hard time as I am not very experienced in writing these kinds of queries.
I want something that I can schedule a ssrs report with- that gives me all the data for each quarter without using real dates. so that I could schedule this to run on Jan 1st, April 1st, July 1st, Oct 1st.
Thank you.
Here is query to get all data for the previous quarter
SELECT *
FROM myTable
WHERE MyDate >= DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, 0)
AND MyDate < DATEADD(qq, DATEDIFF(qq, 0, GETDATE()), 0)
This way you can run it any time after the next quarter starts and before it ends it will still produce same results without having to specify dates.