SQL Question - Column References

  • Hi. Looking for someone to help with a query I'm tring to run.

    I have a table with thousands of rows and need to find all records referenced by one another by a column

    The table looks like the following:

    ID   Barcode   CreateDate   OldBarcode
    1 101 8/10/2022 NULL
    2 102 8/10/2022 NULL
    3 103 8/12/2022 102
    4 104 8/13/2022 103

    I need to be able to get the first date of the original row where the 102 barcode was created. Barcode 103 replaced it on one day, then barcode 104 replaced that one. So I wanted to get to the original barcode. There are other instancnes of this in the same table, so I'd like to be able to get back to that first barcode number for all the rows.

    Hopefully this makes sense.

  • So.. just to be sure from your example data...

    101 has never been changed.

    102 was replaced by 103 which was replaced by 104.

    Is that correct?

    If so, a hierarchical recursive CTE (rCTE) would be the easy thing to do.

     

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • This was removed by the editor as SPAM

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

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