简体   繁体   中英

SQL Query for simple reservation system

I have a simple reservation system. ReservationObject is a flat structure and consist of StartDate, EndDate, Resource and other attributes.

I am trying to get a report of Resource usage. For this report, I am simply querying StartDate to find all the reservations with report query range and counting them.

Example SQL query is..

"SELECT * 
 FROM RequestsQueueObjects 
 WHERE PODObjectID='" +cPOD.PODObjectID + "' 
 and StartDateTime >= '" + StartDate + "' 
 and StartDateTime <= '" + EndDate + "'";

There is an issue with my design. If I have a reservation that starts on 1/1/2015 and ends on 1/15/2015 , This will not show up on report for 1/2/2015 to 1/7/2015

Is there a way to ask SQL to look for all reservation objects that are between two dates ?

EDIT

You want to check if the date ranges overlap. Try the following :

"SELECT * 
 FROM RequestsQueueObjects 
 WHERE PODObjectID='" +cPOD.PODObjectID + "' 
 and StartDateTime <= '" + EndDate+ "' 
 and EndDateTime >= '" + StartDate + "'";

( combine with Steve parameterized query recommendation )

You should use parameterized query with below logic:

"SELECT * 
         FROM RequestsQueueObjects 
         WHERE PODObjectID= @PODObjectID  
         and ((StartDateTime >= @StartDate and EndDateTime <= @EndDate)
         OR (StartDateTime <= @StartDate and EndDateTime >= @StartDate)
         OR (StartDateTime <= @EndDate and EndDateTime >= @EndDate)
         OR (StartDateTime <= @StartDate and EndDateTime >= @EndDate))
    ";

Though above solution works, it can be simplified as below, thanks to link suggested by Thomas Haratyk.

http://logic-and-trick.com/Blog/The-Overlapping-Date-Range-Test

"SELECT * 
             FROM RequestsQueueObjects 
             WHERE PODObjectID= @PODObjectID  
             and EndDateTime >= @StartDate and StartDateTime <= @EndDate
        ";

Use a parameterized query and let the database engine figure out what is the date you pass.
In your code you write the dates between commas and this let the database engine to translate them according to its localization rules. Of course this could result in totally wrong results or in missing records

DateTime startDate = new DateTime(2015, 2, 1);
DateTime endDate = new DateTime(2015, 7, 1, 23, 59, 59); // add time to Include all day

string cmdText = @"SELECT * FROM RequestsQueueObjects 
                   WHERE PODObjectID = @id 
                   and StartDateTime >= @start
                   and StartDateTime <= @end";
using(SqlConnection cn = new SqlConnection(.....))
using(SqlCommand cmd = new SqlCommand(cmdText, cn))
{
    cn.Open();
    cmd.Parameters.Add("@id", cPOD.PODObjectID);
    cmd.Parameters.Add("@start", StartDate);
    cmd.Parameters.Add("@start", EndDate);
    using(SqlDataReader reader = cmd.ExecuteReader())
    {
        ....
    }
}

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