码迷,mamicode.com
首页 > 数据库 > 详细

SQL按分隔符拆分字段串

时间:2018-08-30 11:40:13      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:sele   HERE   des   nio   color   bsp   split   business   var   

CREATE   VIEW [dbo].[Split_BusinessUnit]



AS



    WITH tt



    AS (



       SELECT BusinessUnit.BusinessUnitId



            , BusinessUnit.BusinessUnitId zz_id



            , BusinessUnit.new_name



            , BusinessUnit.ParentBusinessUnitId



            , 1 lev



       FROM   BusinessUnit



       WHERE  IsDisabled = 0



       UNION ALL



       SELECT tt.BusinessUnitId



            , b.BusinessUnitId



            , b.new_name



            , b.ParentBusinessUnitId



            , lev + 1



       FROM   tt



              JOIN BusinessUnit AS b



                  ON tt.ParentBusinessUnitId = b.BusinessUnitId



       WHERE  IsDisabled = 0 )



       , ttt



    AS ( SELECT BusinessUnitId





              , CAST(zz_id AS VARCHAR(60)) zz_id



              , new_name



              , ROW_NUMBER() OVER ( PARTITION BY BusinessUnitId



                                    ORDER BY lev DESC ) lev



         FROM   tt )



    SELECT   BusinessUnitId



           , MAX(lev) lev



           , MAX(CASE WHEN lev = 1 THEN zz_id



                      ELSE NULL



                 END) lev1id



           , MAX(CASE WHEN lev = 1 THEN new_name



                      ELSE NULL



                 END) lev1



           , MAX(CASE WHEN lev = 2 THEN zz_id



                      ELSE NULL



                 END) lev2id



           , MAX(CASE WHEN lev = 2 THEN new_name



                      ELSE NULL



                 END) lev2



           , MAX(CASE WHEN lev = 3 THEN zz_id



                      ELSE NULL



                 END) lev3id



           , MAX(CASE WHEN lev = 3 THEN new_name



                      ELSE NULL



                 END) lev3



           , MAX(CASE WHEN lev = 4 THEN zz_id



                      ELSE NULL



                 END) lev4id



           , MAX(CASE WHEN lev = 4 THEN new_name



                      ELSE NULL



                 END) lev4



           , MAX(CASE WHEN lev = 5 THEN zz_id



                      ELSE NULL



                 END) lev5id



           , MAX(CASE WHEN lev = 5 THEN new_name



                      ELSE NULL



                 END) lev5



           , MAX(CASE WHEN lev = 6 THEN zz_id



                      ELSE NULL



                 END) lev6id



           , MAX(CASE WHEN lev = 6 THEN new_name



                      ELSE NULL



                 END) lev6



           , MAX(CASE WHEN lev = 7 THEN zz_id



                      ELSE NULL



                 END) lev7id



           , MAX(CASE WHEN lev = 7 THEN new_name



                      ELSE NULL



                 END) lev7



           , MAX(CASE WHEN lev = 8 THEN zz_id



                      ELSE NULL



                 END) lev8id



           , MAX(CASE WHEN lev = 8 THEN new_name



                      ELSE NULL



                 END) lev8



           , MAX(CASE WHEN lev = 9 THEN zz_id



                      ELSE NULL



                 END) lev9id



           , MAX(CASE WHEN lev = 9 THEN new_name



                      ELSE NULL



                 END) lev9



           , MAX(CASE WHEN lev = 10 THEN zz_id



                      ELSE NULL



                 END) lev10id



           , MAX(CASE WHEN lev = 10 THEN new_name



                      ELSE NULL



                 END) lev10



    FROM     ttt



    GROUP BY BusinessUnitId;

 

SQL按分隔符拆分字段串

标签:sele   HERE   des   nio   color   bsp   split   business   var   

原文地址:https://www.cnblogs.com/BinBinGo/p/9559080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!