我有两张桌子。
地图
Product Channel ProgDate AdvTime
Air Pix 30-04-2011 20:57:16
Air Pix 30-05-2011 00:55:08
工商管理硕士
Product Channel Date ProgStartTime ProgEndTime
Air Pix 30-04-2011 23:00:00 02:00:00
Air Pix 30-04-2011 21:00:00 22:00:00
我必须检查 MAP 中的 AdvtTime 是否在 MBA 表中的StartTime
&之间。EndTime
但是当持续时间(ProgStartTime
- ProgEndTime
)为一小时时,我需要 + 或 - 5 分钟的缓冲区。
我
00:05:08 to match 23:00:00
需要,02:00:00
并且
。20:57:16 to match 21:00:00
22:00:00
我使用以下查询加入
Select Distinct mb.Id as mbaid,mp.id as mapid, mp.Channel as Channel, mp.Product,mp.ProgDate, mp.AdvTime, mb.Channel, mb.ProgStartTime,
mb.ProgEndTime, convert(time, dateadd(minute, datediff(minute, mb.progStartTime, mb.progEndTime), 0)) as timeDiff from map22 as mp inner join mba22 as mb
on ((mp.ProgDate = mp.ProgDate and mp.Channel=mb.Channel and mp.Product=mb.Product))
where (mp.ProgDate = mb.ProgDate
AND AdvTime >= ProgStartTime
AND (AdvTime <= ProgEndTime OR ProgEndTime < ProgStartTime))
OR
(mp.ProgDate = Dateadd(day,1,mb.ProgDate)
AND ProgEndTime < ProgStartTime
AND AdvTime <= ProgEndTime)
order by mp.Id asc