我有7种略有不同的日期功能,根据创建的& amp;和ydD预订[1月到12月]。到达日期[请参阅下文]。我的问题是,如何轻松改变这些以适应4月至3月的财政年度,而不是1月至12月?
1. --CREATED BY YEAR--LAST YEAR - UP TO TODAY'S DATE (BASED ON *CREATED DATE*)---
COUNT(CASE WHEN (CAST(MASTER_VIEW_TradingReports.CreatedDate AS DATE)>=CAST(DATEPART(yyyy,DATEADD(m,-12,GETDATE())) AS VARCHAR)+'-01-01') AND (CAST(MASTER_VIEW_TradingReports.CreatedDate AS DATE)<DATEADD(m,-12,GETDATE())) THEN MASTER_VIEW_TradingReports.HolidayCost END) AS [LastYearToDateBookingsCreated]
2. --CREATED BY YEAR--THIS YEAR - UP TO TODAY'S DATE (BASED ON *CREATED DATE*)---
COUNT(CASE WHEN MASTER_VIEW_TradingReports.CreatedYear=DATEPART(yyyy,GETDATE()) THEN MASTER_VIEW_TradingReports.HolidayCost END) AS [ThisYearToDateBookingsCreated]
3. --ARRIVALS BY YEAR--LAST YEAR - UP TO TODAY'S DATE (BASED ON *ARRIVAL DATE*, NOT CREATED DATE)---
ISNULL(COUNT(CASE WHEN (DATEPART(yyyy, MASTER_VIEW_TradingReports.ArrivalDate) = DATEPART(yyyy, DATEADD(m, - 12, GETDATE())) AND CAST(MASTER_VIEW_TradingReports.CreatedDate AS DATE) <= DATEADD(m, - 12, GETDATE())) THEN MASTER_VIEW_TradingReports.HolidayCost END), 0) AS [LastYearToDateBookings]
4. --ARRIVALS BY YEAR--THIS YEAR - UP TO TODAY'S DATE (arrivals this year onward, with any createdate up to today)---
ISNULL(COUNT(CASE WHEN (DATEPART(yyyy, MASTER_VIEW_TradingReports.ArrivalDate) = DATEPART(yyyy, GETDATE()) AND CAST(MASTER_VIEW_TradingReports.CreatedDate AS DATE) <= GETDATE()) THEN MASTER_VIEW_TradingReports.HolidayCost END), 0) AS [YearToDateBookings]
5. --ARRIVALS BY YEAR--NEXT YEAR - UP TO TODAY'S DATE (arrivals next year, with any createdate up to today)---
ISNULL(COUNT(CASE WHEN (DATEPART(yyyy, MASTER_VIEW_TradingReports.ArrivalDate) = DATEPART(yyyy, GETDATE()) + 1 AND CAST(MASTER_VIEW_TradingReports.CreatedDate AS DATE) <= GETDATE()) THEN MASTER_VIEW_TradingReports.HolidayCost END), 0) AS [NextYearToDateBookings]
6. --ARRIVALS ANYTIME THIS YEAR THAT WERE CREATED UP TO TODAY'S DATE LAST YEAR--
ISNULL(COUNT(CASE WHEN (DATEPART(yyyy, MASTER_VIEW_TradingReports.ArrivalDate) = DATEPART(yyyy, GETDATE()) AND (DATEPART(yyyy, MASTER_VIEW_TradingReports.CreatedDate) = DATEPART(yyyy, GETDATE())-1 AND CAST(MASTER_VIEW_TradingReports.CreatedDate AS DATE)<=CAST(DATEADD(d,-364,GETDATE()) AS DATE))) THEN MASTER_VIEW_TradingReports.HolidayCost END), 0) AS ThisYearToDateBookingsByArrivalsBookedLastYear
7. --ARRIVALS ANYTIME NEXT YEAR THAT WERE CREATED UP TO TODAY'S DATE THIS YEAR---
ISNULL(COUNT(CASE WHEN (DATEPART(yyyy, MASTER_VIEW_TradingReports.ArrivalDate) = DATEPART(yyyy, GETDATE())+1 AND (DATEPART(yyyy, MASTER_VIEW_TradingReports.CreatedDate) = DATEPART(yyyy, GETDATE()))) THEN MASTER_VIEW_TradingReports.HolidayCost END), 0) AS NextYearToDateBookingsByArrivalsBookedThisYear
答案 0 :(得分:0)
我认为这样的事情对你有用。
BEGIN
DECLARE @LASTENDDATE DATE
DECLARE @LASTSTARTDATE DATE
DECLARE @THISENDDATE DATE
DECLARE @THISSTARTDATE DATE
DECLARE @NEXTENDDATE DATE
DECLARE @NEXTSTARTDATE DATE
-- Last Financial Year
SELECT @LASTENDDATE = DATEADD(dd,-365,(DATEPART(year, GETDATE())||'0331'))
SELECT @LASTSTARTDATE = DATEADD(dd,-364,@LASTENDDATE)
-- This Financial Year
SELECT @THISENDDATE = DATEPART(year, GETDATE())||'0331'
SELECT @THISSTARTDATE = DATEADD(dd,-364,@THISENDDATE)
-- Next Financial Year
SELECT @NEXTENDDATE = DATEADD(dd,+366,(DATEPART(year, GETDATE())||'0331'))
SELECT @NEXTSTARTDATE = DATEADD(dd,-365,@NEXTENDDATE)
1. --CREATED BY YEAR--LAST YEAR - UP TO TODAY'S DATE (BASED ON *CREATED DATE*)---
COUNT(CASE WHEN CAST(MASTER_VIEW_TradingReports.CreatedDate AS DATE) BETWEEN @THISSTARTDATE AND @THISENDDATE THEN MASTER_VIEW_TradingReports.HolidayCost END) AS [LastYearToDateBookingsCreated]
2. --CREATED BY YEAR--THIS YEAR - UP TO TODAY'S DATE (BASED ON *CREATED DATE*)---
COUNT(CASE WHEN MASTER_VIEW_TradingReports.CreatedDate BETWEEN @THISSTARTDATE AND GETDATE() THEN MASTER_VIEW_TradingReports.HolidayCost END) AS [ThisYearToDateBookingsCreated]
3. --ARRIVALS BY YEAR--LAST YEAR - UP TO TODAY'S DATE (BASED ON *ARRIVAL DATE*, NOT CREATED DATE)---
ISNULL(COUNT(CASE WHEN MASTER_VIEW_TradingReports.ArrivalDate BETWEEN @LASTSTARTDATE AND @LASTENDDATE AND CAST(MASTER_VIEW_TradingReports.CreatedDate AS DATE) <= DATEADD(m, - 12, GETDATE()) THEN MASTER_VIEW_TradingReports.HolidayCost END), 0) AS [LastYearToDateBookings]
4. --ARRIVALS BY YEAR--THIS YEAR - UP TO TODAY'S DATE (arrivals this year onward, with any createdate up to today)---
ISNULL(COUNT(CASE WHEN MASTER_VIEW_TradingReports.ArrivalDate BETWEEN @THISSTARTDATE AND @THISENDDATE AND CAST(MASTER_VIEW_TradingReports.CreatedDate AS DATE) <= GETDATE() THEN MASTER_VIEW_TradingReports.HolidayCost END), 0) AS [YearToDateBookings]
5. --ARRIVALS BY YEAR--NEXT YEAR - UP TO TODAY'S DATE (arrivals next year, with any createdate up to today)---
ISNULL(COUNT(CASE WHEN MASTER_VIEW_TradingReports.ArrivalDate BETWEEN @NEXTSTARTDATE AND @NEXTENDDATE AND CAST(MASTER_VIEW_TradingReports.CreatedDate AS DATE) <= GETDATE() THEN MASTER_VIEW_TradingReports.HolidayCost END), 0) AS [NextYearToDateBookings]
6. --ARRIVALS ANYTIME THIS YEAR THAT WERE CREATED UP TO TODAY'S DATE LAST YEAR--
ISNULL(COUNT(CASE WHEN MASTER_VIEW_TradingReports.ArrivalDate BETWEEN @THISSTARTDATE AND @THISENDDATE AND MASTER_VIEW_TradingReports.CreatedDate BETWEEN @LASTSTARTDATE AND DATEADD(dd,-365,GETDATE()) THEN MASTER_VIEW_TradingReports.HolidayCost END), 0) AS ThisYearToDateBookingsByArrivalsBookedLastYear
7. --ARRIVALS ANYTIME NEXT YEAR THAT WERE CREATED UP TO TODAY'S DATE THIS YEAR---
ISNULL(COUNT(CASE WHEN MASTER_VIEW_TradingReports.ArrivalDate BETWEEN @NEXTSTARTDATE AND @NEXTENDDATE AND MASTER_VIEW_TradingReports.CreatedDate BETWEEN @THISSTARTDATE AND GETDATE() THEN MASTER_VIEW_TradingReports.HolidayCost END), 0) AS NextYearToDateBookingsByArrivalsBookedThisYear
END