Table Normalization (Parse comma separated fields into individual records)

前端 未结 4 685
北荒
北荒 2021-01-13 03:31

I have a table like this:

Device

DeviceId   Parts

1          Part1, Part2, Part3
2          Part2, Part3, Part4
3          Part1
         


        
4条回答
  •  生来不讨喜
    2021-01-13 04:10

    If there is a maximum number of parts per device then, yes, it can be done without a cursor, but this is quite complex.

    Essentially, create a table (or view or subquery) that has a DeviceID and one PartID column for each possible index in the PartID string. This can be accomplished by making the PartID columns calculated columns using fn_split or another method of your choice. From there you do a multiple self-UNION of this table, with one table in the self-UNION for each PartID column. Each table in the self-UNION has only one of the PartID columns included in the select list of the query for the table.

提交回复
热议问题