区域预订系统按日期查询日期范围内的最大区域可用性

时间:2016-07-28 05:26:07

标签: mysql sql

我试图按日期实施简单的区域预订申请

我记录了预订请求

总面积是常数:假设100sqft

表:

req_made

STARTDATE |结束日期|面积| storagename

示例:

    01 Aug|__________________________________100_avail________________________|31 Aug
                   06 Aug|____________________________50_____________|25 Aug
                  05 Aug|____40__________|11 Aug
                                             18 Aug|____30_______|23 Aug
                                     13 Aug|_10_|16 Aug
    My Request:                 08 Aug|#######################|20 Aug  

Answer=>Maximum possible within range 10
Area Free:   
          |-------100---||---10----------|-|-40-|50|-------20----|-50|--100--|
                        60                50

Same Diagram in written format (Picture)

我的SQL查询尝试:

SELECT (100-SUM(req_made.area)) AS area,req_made.storagename
 FROM req_made
 WHERE (req_made.enddate >= '2016-08-20' AND
       req_made.startdate <= '2016-08-20')
 OR(req_made.enddate>='2016-08-08' AND
    req_made.startdate<= '2016-08-08')
OR(req_made.enddate<='2016-08-20' AND
    req_made.startdate>= '2016-08-08')
 GROUP BY req_made.storagename

结果 -30 | my_storage

预期 10 | my_storage

0 个答案:

没有答案