Search code examples
databasesqlitecountconflict

How to find count of conflicted date


I want to restrict to user when he enters start and end date between already present start and end dates in the following table.

For example: user tries to insert date start date 2/6/2012 and end date 4/6/2012; this date comes between event e2 from the table below so the user is not allowed to insert it.

 ------------------------------------------------------------------------
 id               event               startdate    enddate
 ------------------------------------------------------------------------
 11                e1                 31/5/2012    1/6/2012
 12                e2                 1/6/2012     4/6/2012
 13                e3                 5/6/2012     6/6/2012
 14                e4                 15/6/2012    16/6/2012

I want to find a count of those events that conflict with other events.


Solution

  • select tbl.startdate, count(tbl.startdate)
    from YourTable tbl
    where 
    exists(select * from YourTable tbl2 where ((tbl2.startdate>=tbl.startdate AND tbl2.startdate<=tbl.enddate) OR (tbl2.enddate>=tbl.startdate AND tbl2.enddate<=tbl.enddate)))
    group by tbl.startdate