CREATE VIEW [dbo].[Split_businessunit] as withTT as ( SELECTBusinessunit.businessunitid, Businessunit.businessunitid zz_id, Businessunit.new_name , Businessunit.parentbusinessunitid,1Lev fromBusinessunitWHEREIsdisabled= 0 UNION All SELECTTT. Businessunitid, B.businessunitid, B.new_name, B.parentbusinessunitid, Lev + 1 fromTTJOINBusinessunit asb onTt. Parentbusinessunitid=B.businessunitidWHEREIsdisabled= 0), TTT as(SELECTBusinessunitid,CAST(zz_id as VARCHAR( -) ) zz_id, New_name, Row_number () Over(PARTITION byBusinessunitidORDER byLevDESC) Lev fromTT)SELECTBusinessunitid,MAX(Lev) Lev,MAX( Case whenLev= 1 Thenzz_idELSE NULL END) Lev1id,MAX( Case whenLev= 1 Thennew_nameELSE NULL END) Lev1,MAX( Case whenLev= 2 Thenzz_idELSE NULL END) Lev2id,MAX( Case whenLev= 2 Thennew_nameELSE NULL END) Lev2,MAX( Case whenLev= 3 Thenzz_idELSE NULL END) Lev3id,MAX( Case whenLev= 3 Thennew_nameELSE NULL END) Lev3,MAX( Case whenLev= 4 Thenzz_idELSE NULL END) Lev4id,MAX( Case whenLev= 4 Thennew_nameELSE NULL END) Lev4,MAX( Case whenLev= 5 Thenzz_idELSE NULL END) Lev5id,MAX( Case whenLev= 5 Thennew_nameELSE NULL END) LEV5,MAX( Case whenLev= 6 Thenzz_idELSE NULL END) Lev6id,MAX( Case whenLev= 6 Thennew_nameELSE NULL END) Lev6,MAX( Case whenLev= 7 Thenzz_idELSE NULL END) Lev7id,MAX( Case whenLev= 7 Thennew_nameELSE NULL END) Lev7,MAX( Case whenLev= 8 Thenzz_idELSE NULL END) Lev8id,MAX( Case whenLev= 8 Thennew_nameELSE NULL END) LEV8,MAX( Case whenLev= 9 Thenzz_idELSE NULL END) Lev9id,MAX( Case whenLev= 9 Thennew_nameELSE NULL END) lev9,MAX( Case whenLev= Ten Thenzz_idELSE NULL END) Lev10id,MAX( Case whenLev= Ten Thennew_nameELSE NULL END) lev10 fromTTTGROUP byBusinessunitid;
SQL Split field string by delimiter