Compare two records of the same table

  • I need to traverse through each record in table and compare between the two records of the same table? Is there a better way of doing this instead of using a cursor?

  • Yes, definitely. Use a self join.

    Can you post table script and tell us a bit more about your issue?

    -- Gianluca Sartori

  • Self join, I mean something like this:

    SELECT A.*

    FROM SomeTable AS A

    INNER JOIN SomeTable AS B

    ON A.SomeColumn = B.SomeOtherColumn

    -- Gianluca Sartori

  • The use of windowed functions comes to mind as well (row_number).

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • join the table to itself is what I would recommend... without code we can't be more specific... rarely is a cursor the best option.

  • SQL_Surfer (9/16/2011)


    I need to traverse through each record in table and compare between the two records of the same table? Is there a better way of doing this instead of using a cursor?

    by "record"...do you mean a column or a complete row of data, eg all columns?

    maybe TableDiff.exe will assist

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

  • Given the table as below

    Col1Col2Col3

    ABABC

    ABCDE

    ABEFG

    I need to be able to display only one row as below

    A B ABC;CDE;EFG

    Can somebody help?

  • It looks like you are trying to concatenate the 3rd column based on the first 2 columns. If so, here is an excellent article on concatenating columns (turning rows into columns):

    http://www.sqlservercentral.com/articles/Test+Data/61572/

    Todd Fifield

  • can you try this?

    declare @a varchar(255) = ''

    select @a = @a + Col1 + Col2 + Col3 + ';'

    from

    (

    select 'A'[Col1], 'B'[Col2], 'ABC'[Col3]

    union all

    select 'A', 'B', 'CDE'

    union all

    select 'A', 'B', 'EFG'

    ) b

    select @a

    "Often speak with code not with word,
    A simple solution for a simple question"

  • Thanks tfifield. The use of FOR XMl mentioned in the article did the trick. Is there any other way by just using the self join?

  • SQL_Surfer (12/7/2011)


    Thanks tfifield. The use of FOR XMl mentioned in the article did the trick. Is there any other way by just using the self join?

    Yes, but not with a variable # of rows. If you knew, for example, that you had an EAV table that always had firstName, LastName, and Address entries, you could self-join your table with those in the ON clauses. With variable sets like this, the FOR XML method is your best bet.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

Viewing 11 posts - 1 through 10 (of 10 total)

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