Click here to Skip to main content
15,921,716 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables.

MAP
Product    Channel  ProgDate   AdvTime 
Air    Pix  30-04-2011  20:57:16
Air    Pix  30-05-2011  00:55:08


MBA
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


I have to check whether AdvtTime in MAP is between the StartTime & EndTime in MBA table.

But when the duration (ProgStartTime - ProgEndTime) is one hour I need a buffer of + or - 5 mins.

I need 00:05:08 to match 23:00:00 to 02:00:00, and 20:57:16 to match 21:00:00 to 22:00:00.

I used the below query to join

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 
Posted
Updated 18-Mar-13 10:27am
v3
Comments
Maciej Los 18-Mar-13 18:33pm    
Which version of MS SQL server?

1 solution

Below example is for MS SQL Server is 2005, so many features available in newest version of MS SQL Server, are not available for 2005 version, like: time, date, datetimeofset, etc.

I prepared example data:
SQL
--declare variable (type of table) for MAP table
DECLARE @map TABLE(Product NVARCHAR(30), Channel NVARCHAR(30), ProgDate DATETIME, AdvTime NVARCHAR(8))
INSERT INTO @map (Product, Channel, ProgDate, AdvTime )
VALUES('Air' , 'Pix' , '2011-04-30' , '20:57:16')
INSERT INTO @map (Product, Channel, ProgDate, AdvTime )
VALUES('Air' , 'Pix' , '2011-04-30' , '00:55:08')

--declare variable (type of table) for MBA table
DECLARE @mba TABLE(Product NVARCHAR(30), Channel NVARCHAR(30), Date DATETIME, ProgStartTime NVARCHAR(8), ProgEndTime NVARCHAR(8))
INSERT INTO @mba (Product, Channel, Date, ProgStartTime, ProgEndTime)
VALUES ('Air' , 'Pix' , '2011-04-30' , '23:00:00' , '02:00:00')
INSERT INTO @mba (Product, Channel, Date, ProgStartTime, ProgEndTime)
VALUES ('Air' , 'Pix' , '2011-05-30' , '21:00:00' , '22:00:00')

--get values:
SELECT FT.*
FROM (
    SELECT Product, Channel, AdvTime, StartTime, 'EndTime' = CASE WHEN EndTime < StartTime THEN DATEADD(d,1,EndTime) ELSE EndTime END
    FROM (
        SELECT t1.Product, t1.Channel, CONVERT(DATETIME, t1.ProgDate + ' ' +  t1.AdvTime) AS AdvTime, CONVERT(DATETIME, t2.Date + ' ' + t2.ProgStartTime) AS StartTime, CONVERT(DATETIME, t2.Date + ' ' + t2.ProgEndTime) AS EndTime
        FROM @map AS t1 LEFT JOIN @mba AS t2 ON t1.Product = t2.Product AND t1.Channel = t2.Channel AND t1.ProgDate = t2.Date
        ) AS DT
    ) AS FT
WHERE (CAST(FT.Advtime AS INT) >= CAST(FT.StartTime AS INT) AND CAST(FT.Advtime AS INT) <= CAST(FT.EndTime AS INT))


You can achieve that using CTE[^] and i would like to show you an example, but... now i'm going to sleep ;)
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900