0

表格1

value

100 x 200 x 300 x 400  `I want to split this row bcoz more than 2 values`
100 x 200
1300 x 1400
200 x 300 x 1200 x 2200  `I want to split this row bcoz more than 2 values`
....

值列不是固定长度的,它也可能包含10 x 200 x 3000 x 5

我想取前 2 个值,然后接下来的 2 个值将转到下一行x

我想像这样拆分行:

预期产出

100 x 200 
300 x 400
100 x 200
1300 x 1400
200 x 300 
1200 x 2200
....

暗示:

我们可以拆分每个 x 的值,然后我们可以连接 2 个值

例如20 x 10 x 5 x 1

像这样拆分20, 10 5, 1然后加入20 x 10, 5 x 1

上面的方法会成功,如果成功意味着我可以得到一些用于拆分和连接行的查询..

我怎样才能在 SQL 中做到这一点?

需要 SQL 查询帮助

4

4 回答 4

2

工作链接

SELECT Rtrim(Substring(value, 1, CHARINDEX( 'x' , value, CHARINDEX( 'x', value) + 1) - 2))
FROM (SELECT value
    FROM table1
    Where LEN(value) - LEN(REPLACE(value, 'x', '')) >= 2) as temp
UNION ALL 
SELECT Substring(value, CHARINDEX( 'x' , value, CHARINDEX( 'x', value) + 1) + 2, LEN(value))
FROM (SELECT value
FROM table1
Where LEN(value) - LEN(REPLACE(value, 'x', '')) >= 2) as temp1
于 2012-11-03T13:19:57.413 回答
1

好吧,如果您想要好的解决方案,那么我认为您需要创建用户定义的函数split,然后再次加入数据(ntile例如使用函数)。如果您真的只想按 2 个元素拆分,那么您可以使用这样的查询

select R.value
from table1 as t
   outer apply (select charindex(' x ', t.value) as c1) as c1
   outer apply (select right(t.value, len(t.value) - c1.c1 - 2) as s1) as s1
   outer apply (select charindex(' x ', s1.s1) as c2) as c2
   outer apply (select case when c2.c2 > 0 then right(s1.s1, len(s1.s1) - c2.c2 - 2) else null end as s2) as s2
   outer apply (
       select case when c2.c2 > 0 then left(t.value, c2.c2 + c1.c1 + 1) else t.value end as value
       union all
       select s2.s2
   ) as R
where R.value is not null

SQL 提琴示例

于 2012-11-03T13:45:03.087 回答
0

试试这个;

SELECT CASE CHARINDEX ('x',value, CHARINDEX('x',value,0)+1) 
        WHEN 0 THEN value 
        ELSE RTRIM(SUBSTRING(value,0, CHARINDEX ('x',value,
       CHARINDEX('x',value,0)+1))) END AS newValue
FROM table1
UNION ALL
SELECT LTRIM(SUBSTRING(value,
        CHARINDEX ('x',value, CHARINDEX('x',value,0)+1)+1,LEN(value))) AS newValue
WHERE CHARINDEX ('x',value, CHARINDEX('x',value,0)+1) >0
FROM table1
于 2012-11-03T13:47:58.543 回答
0

好的,首先你的表必须有 ID。假设 ypu 有这样的表编号:

NumberID Number
1        100 x 200 x 300 x 400
2        100 x 200
3        1300 x 1400    
4        200 x 200 x 200 x 200

解决方案是这个查询:

select * from
    (
    select 
    NumberID AS 'NumberID',
    case when  CHARINDEX('X', Number, CHARINDEX ( 'X' ,Number ) +1)  = 0
                then Number
                else SUBSTRING(Number,   0, LEN(Number) -  CHARINDEX('X', Number, CHARINDEX ( 'X' ,Number ) +1 ))
                END AS Result
    from Numbers

    UNION ALL

    select 
    NumberID AS 'NumberID',
                SUBSTRING(Number,   CHARINDEX('X', Number, CHARINDEX ( 'X' ,Number ) +1 )  +2 , LEN(Number) -  CHARINDEX('X', Number, CHARINDEX ( 'X' ,Number ) +1 ))
                AS Result
    from Numbers
    WHERE CHARINDEX('X', Number, CHARINDEX ( 'X' ,Number ) +1)  <> 0
    ) t1
    ORDER BY t1.NumberID

此查询的结果是:

1   100 x 200
1   300 x 400
2   100 x 200
3   1300 x 1400
4   200 x 200
4   200 x 200

我相信这正是您要寻找的。

于 2012-11-03T14:02:41.247 回答