"Match" entries in two tables with margin of error, with stored procedure

  • I need some help creating a stored procedure that compares two tables, and "matches" the entries.  If it does not match, I'd like to display that seperately.

    This is how I'd like it to run:

    WHERE Table1.PhoneNumber = Table2.PhoneNumber, Table1.Date=Table2.date AND

    WHERE

     Table1.Minutes = Table2.minutes (with a +/-  0.5 margin either way, on either field)

    AND

     

     Table1.Call_Time = Table2.Call_Time (with a +/- 1 minute margin either way, on either field)

    Right now I have a UNION ALL running, which only matches exactly (so the two columns that I want to have a margin of error don't run correctly)

    Any assistance is greatly appreciated.

  • Something like this , Check for syntax

    FROM A JOIN B ON A.PhoneNumber = B.PhoneNumber

    AND A.Date=B.Date AND ((A.Minutes - B.Minutes) NOT BETWEEN (-0.5 AND 0.5))

    AND (DateDiff(mm,A.CallTime,B.CallTime) NOT Between (-1 AND 1))

     

     

     


    Kindest Regards,

    Amit Lohia

  • I believe Amit's logic is reversed. This works:

    select A.PhoneNumber, A.Call_Time, B.Call_Time, A.Minutes, B.Minutes

    from Table1 A inner join Table2 B

    ON A.PhoneNumber = B.PhoneNumber

    where

    (DateDiff(ss,A.Call_Time,B.Call_Time)  Between -60 AND 60)

    and

    ((A.Minutes - B.Minutes) BETWEEN -0.5 AND 0.5)

    Table1:

    PhoneNumberCall_TimeMinutes
    11111111118:30:15 AM1.5
    11111111118:45:23 AM2.5
    11111111119:15:30 AM3.5
    11111111119:30:59 AM4.01

     

    Table2:

    PhoneNumberCall_TimeMinutes
    11111111118:30:15 AM1.99
    11111111118:45:23 AM3.01
    11111111119:16:31 AM3.5
    11111111119:30:00 AM4.5

     

    The query above will only select the first and fourth rows, which is what we want.


    Steve Eckhart

  • If it does not match, I'd like to display that seperately.

    I think he wants the one which does not match or may be both.

    DateDiff(ss,A.Call_Time,B.Call_Time)   is better than DateDiff(mm,A.Call_Time,B.Call_Time)  


    Kindest Regards,

    Amit Lohia

  • Here's what I wound up doing:

    (find unmatched entries)

    FROM         A

    WHERE     (NOT EXISTS

                      (SELECT     *

                        FROM          B

                        WHERE      A.Phone_Number = dbo.ccm.Phone_Number AND A.call_date = B.call_date AND 

                                                       A.Minutes BETWEEN dbo.ccm.minutes - 0.5 AND B.minutes + 0.5 AND abs(datediff(minute, 

                                                       A.Call_Time, B.Call_Time)) <= 1))

    (find matched entries)

    FROM         A INNER JOIN

                         B ON A.PHONE_NUMBER = B.phone_number AND A.CALL_DATE = B.CALL_DATE

    WHERE     (A.MINUTES BETWEEN B.MINUTES - 0.5 AND B.MINUTES + 0.5) AND (ABS(DATEDIFF(minute, A.CALL_TIME, 

                          B.CALL_TIME)) <= 1)

    Thanks for your help, guys!

  • Not exists will be slow and use DATEDIFF(seconds instead of DATEDIFF(minute


    Kindest Regards,

    Amit Lohia

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply