How to create a rolling period-over-period comparison in Redshift SQL - sql

I have the following query that pulls all records from a Redshift table from January 1st of the current year through the final date of the most recent, full quarter.
SELECT *
FROM table
WHERE date_value BETWEEN DATE_TRUNC('year',getdate()) AND DATE_TRUNC('quarter',dateadd(day,-1,getdate()));
I now want to create a period-over-period comparison query that returns all records for the previous n months. Ex. if the first query returns all records for Jan - Jun 2022, this query will return all records for Jul - Dec 2021.
Here is what I have so far, however it currently returns Jan - Jun 2021 instead of the desired date range. I've tried playing around with DATEDIFF() instead of DATEADD() but haven't had any luck with that either. Any help is much appreciated.
SELECT *
FROM TABLE
WHERE date_value BETWEEN DATE_TRUNC('year',dateadd(year,-1,getdate())) AND DATE_TRUNC('quarter',dateadd(year,-1,getdate()));

Related

How do you create dates in SQL without typing them all out and then create an aggregation for trailing weeks?

A question I have is how do you create a temporary table of dates without typing all of them out row after row of cases.
For example, today is the 18 November 2018. How would I create a table or use today's date as a query for my tables. So the 18 November would be the start of a week. And the previous week would be 11-17 November. And the week before would be 3-10 November etc. How can I create these dates and then aggregate these together for example the 11-17 November with 3-10 November for the table?

Postgresql max date in dataset for each month

I have a table with the following columns (in addition to others):
name char
tanggal date
Rows are inserted into this table each day.
How can I get the formatted name for the max date of each month,
for example:
Jan 31, Feb 28, Mar 31, Apr 30,...
I am using Postgresql 8.3
You could use extract to get the month of the date. From there on, it's a straight forward group by query:
SELECT MAX(tanggal)
FROM mytable
GROUP BY EXTRACT (MONTH FROM tanggal)

SQL Between Two Dates Missing End Date (SSRS)

I'm using SSRS 2008r2 to generate reports. Using following WHERE statement in SQL query
WHERE (NonPMJobs.npmJobCreateDate >= #Created_Parameter) AND
(NonPMJobs.npmJobCreateDate <= #Created_Parameter2)
I'm using parameters with the data type of DateTime. Users then select day from a calendar. I want to get results where jobs have been created between date 1 (#Created_Parameter) AND date 2 (#Created_Parameter2) INCLUSIVE.
But results being returned do not include the second date (#Created_Parameter2). If I select 01/07/2013 - 05/07/2013 I get 01, 02, 03, 04 but no 05. If I select 01/07/2013 - 06/07/2013 I get 01, 02, 03, 04, 05.
I've tried using:
WHERE (NonPMJobs.npmJobCreateDate BETWEEN #Created_Parameter AND #Created_Parameter2)
but get same results.
What am I missing here and why isn't WHERE statement inclusive? Any pointers would be very much appreciated.
Well, you need to think about this: a DATETIME like this: 05/07/2013 means the 5th of July (or 7th of May - depending on your locale) at midnight when the day starts (a 00:00 hours, so to speak).
So this does NOT include the events that happen on that day!
The best solution would be to use
WHERE (NonPMJobs.npmJobCreateDate >= #Created_Parameter) AND
(NonPMJobs.npmJobCreateDate < DATEADD(DAY, 1, #Created_Parameter2))
and basically getting everything that's smaller than the next day based on #Created_Parameter2. So for your 5th of July, that would be anything before the 6th of July - which includes all events from the 5th of July.

SQL every month basis report.

I have a 2 year data in SQL and i want to create or write query will be pull out data monthly basis. How do i write query which will give me every month data in one shot.
Jan Feb Mar Apil ....etc
SELECT myField1,myField2,...MONTH(myDateField) as monthDate,YEAR(myDateField) as yearDate
FROM myTableName ORDER BY yearDate,monthDate

Convert GMT dates to BST on the fly in SQL Server 2000

I have a table which contains a value for every 30 minutes in a month, e.g.
20/03/2010 00:00 12
20/03/2010 00:30 14
etc
All of this data is stored in GMT
I need to do a select on this table for the data in bst/clock time
for example
select *
from tbl
where dt between '01 April 2010' and '30 April 2010 23:30'
when in BST as the date range above is the dates need to be converted
I also need a way of taking the changeover hour and the end of March and October into account
Unfortunatly I cannot upgrade SQL server
Is there any way that I can do this in SQL for SQL Server 2000?
something like a function I could do?
select *
from tbl
where fnConvertToClockTime(dt) between '01 April 2010' and '30 April 2010 23:30'
When in GMT the function would return the exact date from the table
Cheers
Rupert
I have the same problem and am investigating the use of Calendar tables for such convserions. Store all dates in UTC time then convert as necessary, I'm probably going to create VIEWS for the different time zones to avoid parameter passing but I guess a stored procedure would also work.
I found some useful information here: how-do-i-convert-local-time-to-utc-gmt-time
And an associated article on Calendar tables here: why-should-i-consider-using-an-auxiliary-calendar-table