Home Forums SQL Server 2012 SQL Server 2012 - T-SQL Join a row from one table to just one row from another table by time. RE: Join a row from one table to just one row from another table by time.

  • itsyourssankalpa (5/29/2016)


    This is what I did.

    I am sure there is more efficient way to do this though.

    if object_id ('tempdb..#Temp') is not null

    drop table #temp

    if object_id ('tempdb..#Temp1') is not null

    drop table #temp1

    if object_id ('tempdb..#Temp2') is not null

    drop table #temp2

    if object_id ('tempdb..#Temp3') is not null

    drop table #temp3

    SELECT a.Time, b.Time AS [Time(B)], Location

    into #temp

    FROM TableA a

    CROSS APPLY (SELECT TOP 1 * FROM TableB ca WHERE ca.Time > DATEADD(minute, 30, a.time) ORDER BY ca.Time ASC) b

    select *, row_number() over (order by Time) as RowNumber into #temp1 from TableA where time not in (Select Time from #temp)

    select *, row_number() over (order by Time) as RowNumber into #temp2 from TableB where time not in (Select [Time(B)] from #temp)

    select c.Time, d.Time as [Time(B)], d.Location

    into #temp3

    from #temp1 c

    join #temp2 d

    on c.RowNumber=d.RowNumber

    select * from #temp

    union

    select * from #temp3

    that doesnt match your expected results......"Dubai" is repeated

    if object_id ('tempdb..tableA') is not null

    drop table tableA

    if object_id ('tempdb..tableB') is not null

    drop table tableB

    if object_id ('tempdb..#Temp') is not null

    drop table #temp

    if object_id ('tempdb..#Temp1') is not null

    drop table #temp1

    if object_id ('tempdb..#Temp2') is not null

    drop table #temp2

    if object_id ('tempdb..#Temp3') is not null

    drop table #temp3

    CREATE TABLE tableA(

    Id INTEGER NOT NULL

    ,Time TIME NOT NULL

    );

    INSERT INTO tableA(Id,Time) VALUES (1,'08:00:00');

    INSERT INTO tableA(Id,Time) VALUES (2,'08:30:00');

    INSERT INTO tableA(Id,Time) VALUES (3,'09:00:00');

    INSERT INTO tableA(Id,Time) VALUES (4,'09:30:00');

    INSERT INTO tableA(Id,Time) VALUES (5,'10:00:00');

    CREATE TABLE tableb(

    Id INTEGER NOT NULL

    ,Time TIME NOT NULL

    ,Location VARCHAR(9) NOT NULL

    );

    INSERT INTO tableb(Id,Time,Location) VALUES (1,'07:05:00','Dallas');

    INSERT INTO tableb(Id,Time,Location) VALUES (2,'08:31:00','London');

    INSERT INTO tableb(Id,Time,Location) VALUES (3,'08:00:00','Paris');

    INSERT INTO tableb(Id,Time,Location) VALUES (4,'09:40:00','Dubai');

    INSERT INTO tableb(Id,Time,Location) VALUES (5,'10:00:00','Frankfurt');

    SELECT a.Time, b.Time AS [Time(B)], Location

    into #temp

    FROM TableA a

    CROSS APPLY (SELECT TOP 1 * FROM TableB ca WHERE ca.Time > DATEADD(minute, 30, a.time) ORDER BY ca.Time ASC) b

    select *, row_number() over (order by Time) as RowNumber into #temp1 from TableA where time not in (Select Time from #temp)

    select *, row_number() over (order by Time) as RowNumber into #temp2 from TableB where time not in (Select [Time(B)] from #temp)

    select c.Time, d.Time as [Time(B)], d.Location

    into #temp3

    from #temp1 c

    join #temp2 d

    on c.RowNumber=d.RowNumber

    select * from #temp

    union

    select * from #temp3

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day