SQL Server Add column with parent tag

niwlg2el  于 2023-11-16  发布在  其他
关注(0)|答案(1)|浏览(95)

I have following table with 100 000 records:

Column FAASID has tags including parent tags, parent tag for tag 005-UPS-17999 is F-005-FRT, parent tag is defined as follows, if value from FAAAID column is the same as in FANUMB column then tag from FAASID will be parent tag. What I need is to add column with parent tag, as below:

FANUMB FAAAID FAASID         Parent_tag
126697 126695 005-UPS-17999  F-005-FRT
126695 106575 F-005-FRT      D-867-IHJ
106575 13456  D-867-IHJ
iszxjhcz

iszxjhcz1#

You might be looking for this:

SELECT t1.FANUMB,t1.FAAID,t1.FAASID,t2.FAASID AS Parent_tag
FROM YourTable AS t1
LEFT JOIN YourTable AS t2 ON t1.FAAAID=t2.FANUMB

The concept is called self-referenced table where a table's FK points to another row in the same table. You can join them like you'd join other tables, but you must use aliases ( t1 and t2 here) to keep them separated.

相关问题