透视空间将varchar分隔为多行(名称 - 值对)

时间:2012-12-04 22:52:01

标签: sql-server sql-server-2008 tsql

我在SQL Server 2008 db中有大量行

对于每一行,我有3列我关心

典型的行如下所示:

AccountNumber    | basecode                      | subcode
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9689787209368901 | AQTXG AQTXG AQTXG AQTXG ACC5Z | ZQ596 ZQ596 ZQ596 ZQ655 ZC655

我需要一个SQL查询,将其转换为以下内容:

AccountNumber      |    basecode| subcode
+++++++++++++++++++++++++++++++++++++++++++
9689787209368901   |    AQTXG   | ZQ596
9689787209368901   |    AQTXG   | ZQ596
9689787209368901   |    AQTXG   | ZQ596
9689787209368901   |    AQTXG   | ZQ655
9689787209368901   |    ACC5Z   | ZC655

2 个答案:

答案 0 :(得分:0)

如果行总共填充了5个序列,那么这应该可以正常工作

Select TableAccountNumber,SubString(basecode,1,5) as BC,SubString(Subcode,1,5) as SC
from @test
UNION ALL
Select TableAccountNumber,SubString(basecode,7,5) as BC,SubString(Subcode,7,5) as SC
from @test
UNION ALL
Select TableAccountNumber,SubString(basecode,13,5) as BC,SubString(Subcode,13,5) as SC
from @test
UNION ALL
Select TableAccountNumber,SubString(basecode,19,5) as BC,SubString(Subcode,19,5) as SC
from @test
UNION ALL
Select TableAccountNumber,SubString(basecode,25,5) as BC,SubString(Subcode,25,5) as SC
from @test

可以包装为

Select * from
(
Select TableAccountNumber,SubString(basecode,1,5) as BC,SubString(Subcode,1,5) as SC,0 as Sequence
from @test
UNION ALL
Select TableAccountNumber,SubString(basecode,7,5) as BC,SubString(Subcode,7,5) as SC,1
from @test
UNION ALL
Select TableAccountNumber,SubString(basecode,13,5) as BC,SubString(Subcode,13,5) as SC,2
from @test
UNION ALL
Select TableAccountNumber,SubString(basecode,19,5) as BC,SubString(Subcode,19,5) as SC,3
from @test
UNION ALL
Select TableAccountNumber,SubString(basecode,25,5) as BC,SubString(Subcode,25,5) as SC,4
from @test
) a
order by TableAccountNumber,Sequence

答案 1 :(得分:0)

由于您使用的是SQL Server,因此可以使用递归查询来拆分数据。在此查询中,您可以使用空格(' ')分割未知数量的项目,如果分隔符更改,则很容易更改。您的查询与此类似:

;with cte (AccountNumber, basecodeItem, basecode, subcodeItem, subcode) as
(
  select AccountNumber,
    cast(left(basecode, charindex(' ',basecode+' ')-1) as varchar(50)) basecodeItem,
         stuff(basecode, 1, charindex(' ',basecode+' '), '') basecode,
    cast(left(subcode, charindex(' ',subcode+' ')-1) as varchar(50)) subcodeItem,
         stuff(subcode, 1, charindex(' ',subcode+' '), '') subcode
  from yourtable
  union all
  select AccountNumber,
    cast(left(basecode, charindex(' ',basecode+' ')-1) as varchar(50)) basecodeItem,
    stuff(basecode, 1, charindex(' ',basecode+' '), '') basecode,
    cast(left(subcode, charindex(' ',subcode+' ')-1) as varchar(50)) subcodeItem,
    stuff(subcode, 1, charindex(' ',subcode+' '), '') subcode
  from cte
  where basecode > '' or subcode > ''
)
select AccountNumber, basecodeItem, subcodeitem
from cte;

请参阅SQL Fiddle with Demo

结果是:

|    ACCOUNTNUMBER | BASECODEITEM | SUBCODEITEM |
-------------------------------------------------
| 9689787209368900 |        AQTXG |       ZQ596 |
| 9689787209368900 |        AQTXG |       ZQ596 |
| 9689787209368900 |        AQTXG |       ZQ596 |
| 9689787209368900 |        AQTXG |       ZQ655 |
| 9689787209368900 |        ACC5Z |       ZC655 |