1

我有两张桌子。

地图

 Product    Channel  Date   Advt Time 
Air    Pix  30-04-2011  22:23:16
Air    Pix  30-04-2011  23:55:08

工商管理硕士

 Product    Channel Date    StartTime EndTime 
Air    Pix  30-04-2011  22:00:00   01:00:00
Air    Pix  30-04-2011  23:00:00   02:00:00

我必须检查 MAP 中的 AdvtTime 是否介于 MBA 表中的 StartTime 和 EndTime 之间。

我使用了以下查询

select distinct * 
from MAP 
inner join MBA on (MAP.Channel     = MBA.Channel 
                   and MAP.Product = MBA.Product 
                   and MAP.ProgDate=MBA.ProgDate) 
where AdvtTime between ti and tii
order by Channel asc

但随着结束时间延长到第二天,它没有给出任何结果。请帮助我如何进一步进行。

我想:

  • 22:23:16匹配22:00:00,01:00:00
  • 23:55:08匹配23:00:0002:00:00
4

3 回答 3

0

您也可以将此选项与简单的 JOIN 一起使用

SELECT DISTINCT *
FROM dbo.map m JOIN dbo.mba m2 ON m.Product = m2.Product
                                 AND m.Channel = m2.Channel
                                 AND m.[Date] = m2.[Date]
                                 AND (m.[Advt Time] > m2.[StartTime] AND m2.[StartTime] > m2.[EndTime] 
                                   OR  m.[Advt Time] BETWEEN m2.[StartTime] AND m2.[EndTime])  

SQLFiddle上的演示

于 2013-03-17T11:10:49.593 回答
0


select distinct * 
from MAP 
inner join MBA on (MAP.Channel     = MBA.Channel 
                   and MAP.Product = MBA.Product 
                   and MAP.ProgDate=MBA.ProgDate) 
where (AdvtTime between ti and tii) or (AdvTime between ti and DATEADD(hh, DATEDIFF(HH,AdvTime, tii),tii))
order by Channel asc

于 2013-03-17T08:04:45.453 回答
0

以下查询应为您提供所需的答案:

SELECT DISTINCT *
FROM MAP
INNER JOIN MBA
ON MAP.Channel = MBA.Channel
AND MAP.Product = MBA.Product
WHERE (MAP.Date = MBA.Progdate
       AND AdvtTime >= StartTime
       AND (AdvtTime <= EndTime OR EndTime < StartTime))
      OR
      (MAP.Date = dateadd(DAY,1,MBA.ProgDate) -- extended from yesterday
       AND EndTime < StartTime                -- iff EndTime < StartTime
       AND AdvtTime <= EndTime)
ORDER BY MBA.Channel ASC

请参阅http://www.sqlfiddle.com/#!6/1e49f/1/0进行测试。

但是,最好将日期和时间组合到日期时间字段中,然后在需要时提取单独的部分以进行演示。

于 2013-03-17T10:02:23.467 回答