-1

我有一个来自 SELECT 语句的结果集,如何在没有任何分隔符的情况下拆分一列这是我的结果

Size TCount TDevice
 2     5      E01
 4.5   3      W02E01

我想要这个

Size TCount  TDevice
 2      5     E
 2      5     0
 2      5     1
4.5     3     W
4.5     6     0      (we have 2 times of 0)
4.5     3     2
4.5     3     1    

谢谢你

4

2 回答 2

3

您可以加入辅助号码表。我将 spt_values 用于演示目的,但您应该创建一个永久的

WITH Nums
     AS (SELECT number
         FROM   master..spt_values
         WHERE  type = 'P'
                AND number BETWEEN 1 AND 1000),
     Result(Size, TCount, TDevice)
     AS (SELECT 2,  5,'E01'
         UNION ALL
         SELECT 4.5,3,'W02E01')
SELECT Size,
       COUNT(*) * TCount             AS TCount,
       SUBSTRING(TDevice, number, 1) AS TDevice
FROM   Result
       JOIN Nums
         ON Nums.number <= LEN(TDevice)
GROUP  BY Size,
          TCount,
          SUBSTRING(TDevice, number, 1) 
于 2013-10-02T12:44:26.667 回答
1
;with cte as
(
 select Size,TCount,
         substring(TDevice, 1, 1) as Chars,
         stuff(TDevice, 1, 1, '') as TDevice
  from t1
  union all
  select Size,TCount,
         substring(TDevice, 1, 1) as Chars,
         stuff(TDevice, 1, 1, '') as TDevice
  from cte
  where len(TDevice) > 0
)
select distinct Size,sum(TCount),Chars
from cte
group by Size,Chars

SQL小提琴

优点:不需要User defined function (UDF)创建任何东西。

于 2013-10-02T13:08:52.667 回答