Given a data with list of a months worth of data, I need to calculate the sum for past 2 weeks. I could do this by breaking the list in 14 days and then 7 days and calculate the sum for each.
Is there an elegant way to achieve above?
example of data list looks like below:
startDate 2012-05-01
value 1
startDate 2012-05-02
value 1
........
startDate 2012-05-31
value 1
Thanks.
Do you mean you want the sum for each of the past two weeks, or a single value across the past two weeks? The latter is easier than the former:
Note that it’s important to evaluate
DateTime.Todayonce here, as otherwise if the query executes “around” midnight you could end up with an inconsistent set of comparisons.If your data doesn’t include any future values, you can remove the
endpart of course. You should also carefully consider whether you really want to subtract 13 days or 14.EDIT: Okay, now you’ve clarified the requirement, it sounds like the simplest approach would actually be to execute two queries – one for the past 7 days, and one for the 7 days before that. Alternatively, you could use do it via grouping: