SQL Server - Quarterly data Report - sql

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.

Related

SQL Query Last Three Full Months (Excluding Current Month)

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)

not getting previous month record using datepart in sybase

Using below depart syntax to fetch for previous month record, it is working fine till previous year,however it is giving 0 value in January month.How can we get pervious month with date part even if year is change ?
DATEPART(month(GETDATE()) -1
I understand that I used another type of DB, but I want to give a hint. I am using sql server 2019.
Firstly, you need to substitute date and only then take datepart from it.
Queries:
--dateadd -1 would subtract 1 from current month
--(Jan - 1 2022), would be December 2021
select datepart(month, dateadd(month, -1, getdate()))
--also date add covers internally the problem with 30,31 days.
--May always has 31 days, April 30. So -1 subtraction from 31th of May,would result in 30th of April.
select dateadd(month, -1, cast('2021-05-31 10:00:00' as datetime))

How to pull week to date data in SQL Server?

I'm trying to pull week to date data for a recurring report that needs to go out daily. For eg: report that goes out Monday needs to have data for Monday, report that goes out Tuesday will have data for Monday and Tuesday etc. for the current week.
I know how to pull last X days data with :
my_date > DATEADD(day, -6, GETDATE())
How do I pull only week to date?
Use below SQL code in where condition:
where
my_date > DATEADD(dd, -((DATEPART(WEEKDAY, mydate) + 5) % 7), DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0))
The correct adjustment requires a bit of extra modular arithmetic and so the proper number of days to adjust backward works out to the expression below. I am also assuming that datefirst is set to Sunday:
-((DATEPART(WEEKDAY, mydate) + 5) % 7)
If you just subtract two from the day number given by datepart() then you'll end up with a negative number on Sundays which then cause dateadd() to jump forward. You can see a listing of dates and comparison here:
https://rextester.com/IUY88999

SQL Server 2008 SQL Query to retrieve 'working week' (Tuesday to Tuesday)

There are quite a few posts on this topic, but we are still struggling to get some SQL that works for the period and the posts are just confusing.
What we want to do is return all records where timestamp is >= start of working week which is a Tuesday. Could someone advise where we are going wrong.
i.e Yesterday it was 27th October, so when we ran our query it would return all results from 21st October (the previous Tuesday). Today it is 28th, so when we now run our query, it will only return results from the 28th. Tomorrow when we run it, we will get 28th and 29th....etc.
timestamp >= dateadd(day, -4-datepart(dw, getdate()), CONVERT(date,getdate()))
Note: this query worked fine when we ran it yesterday, so I am wondering if it changes by the day?
Mod 7 please:
select
dateadd(day, -(datepart(dw, getdate() ) + 4)%7, getdate())
,dateadd(day, -(datepart(dw, '2014-10-28' ) + 4)%7, '2014-10-28') --2014-10-28
,dateadd(day, -(datepart(dw, '2014-10-27' ) + 4)%7, '2014-10-27') --2014-10-21
,dateadd(day, -(datepart(dw, '2014-10-29' ) + 4)%7, '2014-10-29') --2014-10-28
UPDATE
According to #hvd's comment, when we use these approach, we assuming the start day of week is Sunday by default of U.S.English. Further information in this document.
SET DATEFIRST 7 can ensure this approach to be correct.
Finally understood your points, you should provide an extra parameter here to point out which day is the beginning of working week, then it will be easy to calculate the days

SQL server - Select all items with a date on the previous month

I have a table in my SQL Server database called "items" which has a column called "dateFinished".
I have a script that will run on the 1st day of each month which needs to select all items that finished in the previous month.
So, for example, on the 1st February it will need to select all items where the dateFinished is greater than or equal to 00:00 on the 1st of January and less than 00:00 on 1st February.
it also needs to work across new years (e.g. DEC - JAN).
Any ideas?
Select *
from items
where datefinished >= dateadd(m, datediff(m, 0, GETDATE()) - 1, 0)
AND datefinished < dateadd(m, datediff(m, 0, GETDATE()), 0)
You could get a day of the previous month with dateadd(m,-1,getdate()). Then, filter on the year and month of that date in a where clause, like:
select *
from items
where datepart(yy,dateFinished) = datepart(yy,dateadd(m,-1,getdate()))
and datepart(m,dateFinished) = datepart(m,dateadd(m,-1,getdate()))
This should work across years, and also if the query is run on a later day than the first of the month.
Simple just use what I just used: DATEDIFF(mm,dateFinished,GETDATE()) = 1
SELECT *
FROM items
WHERE DATEDIFF(mm,dateFinished,GETDATE()) = 1
I would start by checking out the DATEADD function
http://msdn.microsoft.com/en-us/library/ms186819.aspx