I'm using C#, NHibernate, Fluent NHibernate and LINQ (repository pattern).
Fictional story on what I want to do
Say I have a car parking place, so I have a car that is renting a space from date until date. This becomes one entry into the database.
I would like to find out how many cars there were for each day over the period of last 30 days. So data that can be used in a trend chart.
Description
What I have is entries in the database that are like so:
Entry:
--------
ID
StartDate
EndDate
I need to find out how many entries there were each day
for a time period (say last 30 days
).
If I have say this data in the database:
ID | Start | End
---|------------|----------
1 | 2013-01-01 | 2013-01-01
2 | 2013-01-02 | 2013-01-02
3 | 2013-01-02 | 2013-01-03 <-- NOTICE
4 | 2013-01-03 | 2013-01-03
5 | 2013-01-03 | 2013-01-05 <-- NOTICE
6 | 2013-01-04 | 2013-01-05
I have date range 2013-01-01
until 2013-01-05
.
I except the result:
Date | Value | Entry IDs (not part of the result, only here for clarity)
-----------|-------|-----------
2013-01-01 | 1 | 1
2013-01-02 | 2 | 2 3
2013-01-03 | 3 | 3 4 5
2013-01-04 | 2 | 5 6
2013-01-05 | 2 | 5 6
I currently only group by startdate
and then get the numbers, but this is invalid now because of this new requirement that was added with enddate
.
I could do a for each day in that range, try to find entries that match in that, but I'm thinking that would be too expensive on the database or web service.
Advices?
You can use the Time Period Library for .NET to calculate the trend data:
Maybe something like this:
Start time and end time are inclusive.
EDIT
I've found this (actually i've a similar case in my project):
Try test it and maybe you can acomodate to your case (note that filtering starts by the date range).