0

我在 SQL Server 2008 数据库中有大量行

对于每一行,我有 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
4

2 回答 2

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
于 2012-12-04T23:04:33.847 回答
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

结果是:

|    ACCOUNTNUMBER | BASECODEITEM | SUBCODEITEM |
-------------------------------------------------
| 9689787209368900 |        AQTXG |       ZQ596 |
| 9689787209368900 |        AQTXG |       ZQ596 |
| 9689787209368900 |        AQTXG |       ZQ596 |
| 9689787209368900 |        AQTXG |       ZQ655 |
| 9689787209368900 |        ACC5Z |       ZC655 |
于 2012-12-05T10:13:19.613 回答