Update statement Syntax (PostgreSQL-Redshift) - sql

In the time dimension we have added additional two columns called Fiscal Year and Fiscal Year Month which are to be populated. In our case Fiscal year starts from
Sep 01 and ends on Aug 31. (For example - Sep 01, 2017 to Aug 31,2018 is Fiscal Year 2018). I am trying to write an update statement in PostgreSQL.
(Logic - case when X.monthname in ('September','October','November','December') then (X.Year+1) else X.Year end)- But looks like I am not able to get the right syntax. Can you help build it ?
We have Date, Year, MonthName columns in our Time table which can be used in update statement.
Thanks.

I think the update would look like:
update x
set Year = Year + 1
where x.monthname in ('September', 'October', 'November', 'December') ;

Related

After certain week of 2022 and continue with this new year

I would like to request some advice about how to set a Where Condition, but after a certain week
What I mean is:
I have dirty data before a specific week of 2022, so I made this:
DATEPART(WK, SA.FECHAE) >= 44
AND
YEAR(SA.FECHAE) >= 2022
But, We're on 2023, so, I need to add the new information of this new year year too into the query.
The query result shows me until 12-31-2022 and need it until today after the week 44 of 2022
...
WHERE (
DATEPART(WEEK, SA.FECHAE) >= 44
AND YEAR(SA.FECHAE) = 2022
)
OR (
YEAR(SA.FECHAE) >= 2023
)
In the OPs question they ask how to add an additional date range to their WHERE clause. The addition of this OR allows a second date range (in this case anything where the year is greater than or equal to 2023) to match the predicate and be returned, without impacting the original.
Plain English definition of the amended where clause:
Week 44 of 2022, or any week of any year from 2023 forward.

Custom Sort on DimCalendar

I have a DimCalendar dimension and I want to create a custom column on top of this which will be used as a "Sort By Column" in PowerBI report.
The sorting order should be Jan 2015, Jan 2016, Jan 2017, Feb 2015, Feb 2016, Feb 2017 and so on.
Hence, can someone help me write a SQL statement to create a column which will rank the numbers in the above sorting order?
Thanks.
[UPDATE]
Sample data - I have taken only first two dates from entire month.
The customer sort can be set by using the year and month. In many databases, you can define it as:
update dimCalendar
set customsort = month(date) * 10000 + year(date);
The ANSI standard syntax would be:
update dimCalendar
set customsort = extract(month from date) * 10000 + extract(year from date);
You can concatenate year part and month part of date.
In Ms SQL server you can use
Datepart (year, date)+"/" + Datepart(month,date)

SQL - check if an order date occurs after the second Saturday in July

I am querying against a table of 4 yrs of order transactions (pk = order number) and I'm looking to tag each record with particular date flags based on the order date - e.g., calendar year, calendar month, fiscal year, etc. There are date attributes that are specific to our business (e.g., not easily solved by a datepart function) that I'm having trouble with.
I was able to add "School Year" (for us that runs Aug 1 - July 31) using a case statement:
case
when datepart(month, oline.order_date_ready) between 8 and 12 then datepart(year, oline.order_date_ready)
else (datepart(year, oline.order_date_ready)-1)
end as school_yr
So for 1/19/2017, the above would return "2016", because to us the 2016 school year runs from Aug 1 2016 to July 31 2017.
But now I'm having trouble repeating the same kind of case statement for something called "Rollover Year". All of our order history tables are reset/"rolled over" on the 2nd Saturday in July every calendar year, so for example the most recent rollover date was Saturday July 9th 2016. Click to view - rollover year date ranges
My above case statement doesn't apply anymore because I can't just add "datepart(month, oline.order_date_ready) = 7" - I don't need the whole month of July, I just need all the orders occurring after the 2nd Saturday in that July. So in this example, I need everything occurring from Sat July 9 2016 to today to be flagged as rollover_date = 2016.
Is there a flexible way to do this without hard coding previous/future rollover dates into another table? That's the only way I can think to solve it currently, but I'm sure there must be a better way.
Thanks!
If you ask for the day-of-the-week of July 1st, then from there it's simple arithmetic, right? This query gives results matching your image:
SELECT y,
CONCAT(y, '-07-01')::timestamp +
CONCAT(6 - EXTRACT(DOW FROM CONCAT(y, '-07-01')::timestamp) + 7, ' days')::interval
FROM generate_series(2013, 2020) s(y)
ORDER BY y DESC
;
So given any date d from year y, if it comes before the 2nd Saturday of July, give it fiscal year y - 1. Otherwise give it fiscal year (school year?) y.

Get Year of Previous Month (For Jan 1st)

I'm currently building a YTD report in SSRS. I'm looking to edit the default "FROM" date in a calendar selection.
I'm looking to retrieve January 1st of the previous months year. For example:
(If it's Feb 16th, 2016 .. the result should be 1/1/2016
If it's Jan 10th, 2016 .. the result should be 1/1/2015)
I built this to retrieve the current year for jan 1st, but it causes issues if we're in January because I need it to retrieve the year of the previous month (in that case it would be 2015, not 2016).
Thanks!
Try this, it should work
=DateAdd(DateInterval.Month,-1,DateSerial(Year(Today), Month(Today), 1))
UPDATE:
Based on your comment I've created this expression. It is not tested but should work.
=IIF(Today.Month>1,
DateAdd(DateInterval.Month,-1,DateSerial(Year(Today), Month(Today), 1)),
DateAdd(DateInterval.Year,-1,DateSerial(Year(Today), Month(Today), 1))
)
Let me know if this helps.
select cast(cast(year(dateadd(mm, -1,getdate())) as varchar)+'-01-01' as date)
replace getdate() with which ever field you're basing this calculation on.
for testing:
select cast(cast(year(dateadd(mm, -1,'2015-01-22')) as varchar)+'-01-01' as date)
select cast(cast(year(dateadd(mm, -1,'2016-02-01')) as varchar)+'-01-01' as date)
select cast(cast(year(dateadd(mm, -1,'2015-12-12')) as varchar)+'-01-01' as date)
We want to use Date Serial which has the forma
=DateSerial(YYYY,MM,DD)
The Month is always January
The day is always the first
If the month is January, it's last year. Otherwise, it's this year.
So, assuming we have an SSRS report with a parameter called Date , we can create a new field for your January date as follows:
=iif(Month(Parameters!Date.Value)=1,
dateserial(Year(Parameters!Date.Value)-1,1,1),
dateserial(Year(Parameters!Date.Value),1,1))
if you want to do this in the query with T-SQL (version 2012) or later:
case when month(#DATE) = 1
then DATEFROMPARTS(YEAR(#DATE)-1,1,1)
else DATEFROMPARTS(YEAR(#DATE),1,1)
end
OR, in earlier versions
CASE WHEN MONTH(#DATE) = 1
THEN CAST(CAST((YEAR(#DATE)-1)*10000 + 101 AS CHAR(8)) AS DATE)
ELSE CAST(CAST((YEAR(#DATE)*10000+ 101) AS CHAR(8)) AS DATE)
END

Previous Year's End Value

I'm trying to retrieve the previous year's end value of a measure.
I know this code gets the value of the previous year, but at the same point in time during that year (so Mar 2012 looks to Mar 2011).
([Measures].[MeasureName], ParallelPeriod([Time].[Calendar].[Year]))
I'd like any date in 2012 to look at the last value in 2011 (Dec 2011). So if we're looking at the Year level of 2012 or any Month level, it all points to Dec 2011.
where are you trying to do this? On a MDX query? Or on a KPI?
Like you can access the value of Dec 2011 like this for example:
ParallelPeriod([Time].[Calendar].[Year], 1, [Time].[Calendar].[Year].[December 2012])
Combine the time functions:
ClosingPeriod([Time].[Calendar].[Month], Ancestor(ParallelPeriod([Time].[Calendar].[Year], 1), [Time].[Calendar].[Year]) )