全くアイデアのおかげで、しかし、ショーン・ランゲとpivot
でコメントで述べたように、あなたはあなたの選択の文字列分割機能を使用して、問題の出力を得ることができ、よりあなたはできinsert
テーブルなどへ:
declare @string nvarchar(1000) = 'TM_15 <= 0.574803 AND TM_3 <= 0.4 AND TM_9 <= 0.298738 AND TM_8 <= 0.30605
AND TM_16 <= 0.367816 AND TM_12 <= 0.341463 AND TM_10 <= 0.379487 AND TM_4 <= 0.44186
AND TM_2 <= 0.45 AND TM_7 <= 0.247525 AND TM_1 <= 0.6 AND TM_13 <= 0.28 AND TM_5 <= 0.285714
AND TM_11 <= 0.6 AND TM_6 <= 0.448276 AND TM_15 <= 0.285047 AND TM_14 <= 0.6
AND TM_12 <= 0.248366 AND TM_5 <= 0.245399 AND TM_16 <= 0.09375 AND TM_3 <= 0.372093
AND TM_15 <= 0.199713 AND TM_10 <= 0.326996 AND TM_9 <= 0.248731 AND TM_8 <= 0.279412
AND TM_13 <= 0.248577 AND TM_4 <= 0.311475 AND TM_2 <= 0.145833 AND TM_7 <= 0.213483
AND TM_11 <= 0.199029 AND TM_4 <= 0.165289'
select r
,[TM_0]
,[TM_1]
,[TM_2]
,[TM_3]
,[TM_4]
,[TM_5]
,[TM_6]
,[TM_7]
,[TM_8]
,[TM_9]
,[TM_10]
,[TM_11]
,[TM_12]
,[TM_13]
,[TM_14]
,[TM_15]
,[TM_16]
from (
select row_number() over (partition by left(s.item,charindex(' ',s.item,1)-1) order by s.rn) as r
,left(s.item,charindex(' ',s.item,1)-1) as c
,substring(s.item,charindex(' ',s.item,1)+1,9999) as v
from dbo.fn_StringSplitMax(@string,'and ',null) as s
) as s
pivot (max(s.v) for c in([TM_0]
,[TM_1]
,[TM_2]
,[TM_3]
,[TM_4]
,[TM_5]
,[TM_6]
,[TM_7]
,[TM_8]
,[TM_9]
,[TM_10]
,[TM_11]
,[TM_12]
,[TM_13]
,[TM_14]
,[TM_15]
,[TM_16]
)
) as p
order by r
出力:
+---+------+---------+--------------+----------------+---------------+----------------+--------------+----------------+----------------+--------------+--------------+--------------+--------------+--------------+-----------+--------------+--------------+
| r | TM_0 | TM_1 | TM_2 | TM_3 | TM_4 | TM_5 | TM_6 | TM_7 | TM_8 | TM_9 | TM_10 | TM_11 | TM_12 | TM_13 | TM_14 | TM_15 | TM_16 |
+---+------+---------+--------------+----------------+---------------+----------------+--------------+----------------+----------------+--------------+--------------+--------------+--------------+--------------+-----------+--------------+--------------+
| 1 | NULL | <= 0.6 | <= 0.45 | <= 0.4 | <= 0.44186 | <= 0.285714 | <= 0.448276 | <= 0.247525 | <= 0.30605 | <= 0.298738 | <= 0.379487 | <= 0.6 | <= 0.341463 | <= 0.28 | <= 0.6 | <= 0.574803 | <= 0.367816 |
| 2 | NULL | NULL | <= 0.145833 | <= 0.372093 | <= 0.311475 | <= 0.245399 | NULL | <= 0.213483 | <= 0.279412 | <= 0.248731 | <= 0.326996 | <= 0.199029 | <= 0.248366 | <= 0.248577 | NULL | <= 0.285047 | <= 0.09375 |
| 3 | NULL | NULL | NULL | NULL | <= 0.165289 | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | <= 0.199713 | NULL |
+---+------+---------+--------------+----------------+---------------+----------------+--------------+----------------+----------------+--------------+--------------+--------------+--------------+--------------+-----------+--------------+--------------+
文字列分割機能:
create function [dbo].[fn_StringSplitMax]
(
@str nvarchar(max) = ' ' -- String to split.
,@delimiter as nvarchar(max) = ',' -- Delimiting value to split on. Can be multiple characters.
,@num as int = null -- nth value will be returned. NULL returns all.
)
returns table
as
return
with s as
( -- Convert the string to an XML value, replacing the delimiter with XML tags
select convert(xml,'<x>' + replace((select @str for xml path('')),@delimiter,'</x><x>') + '</x>').query('.') as s
)
select rn
,item -- Select the values from the generated XML value by CROSS APPLYing to the XML nodes
from(select row_number() over (order by (select null)) as rn
,n.x.value('.','nvarchar(max)') as item
from s
cross apply s.nodes('x') as n(x)
) a
where rn = @num
or @num is null;
MySQLまたはMS SQL Serverを使用していますか? – jarlh
使用している正しいデータベースにタグを付けてください。MySQLはSQLサーバと同じではありません。文字列にはTM_1はありますが、結果にはTM_1が2つあります。出力.... –
ねえ、あなたの迅速な返答のためにありがとう。タグについては申し訳ありませんが、これはスタックに関する最初の質問です。それはSQL Serverに関係します。文字列と出力の関係については、ページの書式設定が少し動いています。私は一般的な出力を達成するために探しています。 一般に、「TM_0-16」列の場合、複数の値が割り当てられている可能性があります。私はそのような形式のテーブルにそれらを得ることに苦労しています。 – Marc