问题
I am trying to get Which day of the quarter when you give a current date.
For Example if i give 01/25/2012 then the output should be 25. Since it is the 25th day of the quarter.
Similarly if i give 02/01/2012 it should give 32 as output.
I am able to get the first day of the quarter but not able to get which day it is in the quarter.
Platform is Teradata. The Calendar does not have an option for day_of_quarter which is what I am looking for. Given the date, I require to know the number of day of the quarter.
Please help. Thank you in advance.
回答1:
TRUNC(datecol)
returns the first day of the quarter,
date1 - date2
returns the number of days between:
SELECT (datecol - TRUNC(datecol, 'Q')) + 1 AS day_of_quarter
You might put this calculation into a SQL UDF or add it as a column to your existing calendar table.
回答2:
This is based on calendar quarter. You can adjust the case statement if you have some sort of business calendar.
select
c.*,
CASE WHEN MONTH(calendar_date) BETWEEN 1 AND 3
THEN CAST(((calendar_date / 10000) * 10000) + 101 AS DATE)
WHEN MONTH(calendar_date) BETWEEN 4 AND 6
THEN CAST(((calendar_date / 10000) * 10000) + 401 AS DATE)
WHEN MONTH(calendar_date) BETWEEN 7 AND 9
THEN CAST(((calendar_date / 10000) * 10000) + 701 AS DATE)
WHEN MONTH(calendar_date) BETWEEN 10 AND 12
THEN CAST(((calendar_date / 10000) * 10000) + 1001 AS DATE)
END as First_Day_Of_Quarter,
calendar_date - first_day_of_quarter + 1 as Day_Of_Quarter
from
sys_calendar.calendar c
where
year_of_calendar = 2014
order by 1 asc
来源:https://stackoverflow.com/questions/27242552/getting-the-number-of-day-in-quarter-teradata