简体   繁体   English

年初至今-考虑到上一年记录中的时间戳和日期

[英]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. 我的问题是,我需要在WHERE子句中进行哪些更改,以便SP能够考虑到CreatedDate字段中的时间戳以及日期本身。 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. 例如...如果报表今天在今天下午1点运行,则只会带回去年[2015]这个时候今天下午1点之前的记录,并且显然会带回迄今为止在2016年创建的所有记录。 At present, 2015 is bringing back all records up to the end of the day. 目前,2015年将所有记录恢复到一天结束。 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') 无论如何,对年份的引用似乎是不必要的,并且您还可以在像这样的之间包含空值-其中,isull(@ createdFrom16,'20160101')和isull(@ CreatedTo16,'20161231')之间的CreatedDate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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