Row matching in Tables

  • Hi,

    I have two tables , Table_A and Table_B and the same rows in both the tables. The rows have a column called ID which has multiple rows corresponding to it.

    Columns are

    Id CourseId Group Starttime EndTime Facultyid

    I am trying to match every row in the first table with every row in the second table.

    Eg :

    If the Table_A.Starttime <> Table_B.Starttime ,where Table_A.ID=Table_B.ID, I want to show an error "Starttime does not match"

    If the Table_A.Endtime <> Table_B.Endtime ,where Table_A.ID=Table_B.ID, I want to show an error "Endtime does not match"

    I am illustarting an example below:

    Table_A

    Id CourseId Group Starttime EndTime Facultyid

    123 AAA 3 0800 0930

  • Is this homework? If so, can you be explict about what kind of help your school's policy allows us to give you?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • SELECT A.ID,

    CASE

    WHEN a.StartTime != b.StartTime THEN 'Start Time doesn't match'

    WHEN a.EndTime != b.EndTime THEN 'End Time doesn't match'

    ELSE 'Record Not found in TableB'

    END

    FROM

    TableA a INNER JOIN TableB b ON a.ID = b.ID

    hope the above query will help you..:)

  • ;with wcte (AId, ACourseId, AGroup, AStarttime, AEndTime, AFacultyid,BId, BCourseId, BGroup, BStarttime, BEndTime, BFacultyid)

    Select A.ID AId, A.CourseId ACourseId, A.Group AGroup, A.Starttime AStarttime, A.EndTime AEndTime, A.Facultyid AFacultyid,

    B.ID BId, B.CourseId BCourseId, B.Group BGroup, B.Starttime BStarttime, B.EndTime BEndTime, B.Facultyid BFacultyid

    FROM TABLE_A A INNER JOIN TABLE_B B OB B.ID = A.ID)

    SELECT AId, ACourseId, AGroup, AStarttime, AEndTime, AFacultyid,

    CASE

    WHEN ASTARTTIME <> BSTARTTIME THEN 'START TIEM DO NOT MATCH'

    WHEN AENDTIME <> BENDTIME THEN 'END TIEM DO NOT MATCH'

    ELSE ''

    END

    FROM

    WCTE

    i hope it will help you.

    Atif Sheikh

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Sometimes, winning is not an issue but trying.
    You can check my BLOG
    [font="Arial Black"]here[/font][/url][/right]

Viewing 4 posts - 1 through 3 (of 3 total)

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