简体   繁体   中英

YoY & YTD - Taking into account the timestamp, as well as date, on records in the previous year

My question is, what do I need to change in the WHERE clause for the SP to take into account the timestamp, as well as the date itself, on the CreatedDate field. For example...If the report was run at 1pm today, it would only bring back records made up to 1pm today this time last year [2015] and obviously all records created in 2016 so far. At present, 2015 is bringing back all records up to the end of the day. Any help would be much appreciated. Claire

@CreatedFrom15 DateTime = NULL,
@CreatedTo15 DateTime = NULL,
@CreatedFrom16 DateTime = NULL,
@CreatedTo16 DateTime = NULL,


SELECT

BookingID,
CreatedYear,
CreatedDate,
NightDuration,
HolidayCost,
TRAVPropertyTypeCategory

FROM Vw_TRAVLodgeResortGlampingFinancialByNights

WHERE 
((CreatedYear = DATEPART(YEAR,GETDATE())-1 AND CreatedDate BETWEEN @CreatedFrom15 AND @CreatedTo15) AND (CreatedDate  >= @CreatedFrom15 OR @CreatedFrom15 IS NULL) AND (CreatedDate <= @CreatedTo15 OR @CreatedTo15 IS NULL)
OR
(CreatedYear = DATEPART(YEAR,GETDATE()) AND CreatedDate BETWEEN @CreatedFrom16 AND @CreatedTo16) AND (CreatedDate  >= @CreatedFrom16 OR @CreatedFrom16 IS NULL)AND(CreatedDate <= @CreatedTo16 OR @CreatedTo16 IS NULL))

can you specify the dates of the parameters? anyhow, the reference to the year seems unnecessary and also you can include the null case inside the between like this- where CreatedDate between isnull(@createdFrom16,'20160101') and isnull(@CreatedTo16,'20161231')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM