これは可能な動的ソリューションです。結果の表の列数は入力データに依存することに注意してください。これは通常は悪い考えです。このため、このコードをストアドプロシージャ内に配置するのは難しいでしょう。それにもかかわらず、ここで
は、あなたの質問に答える必要がありTSQL断片である:
--create a custom type that will be used in the final dynamic call
if exists (select * from sys.types where name = 'TestTableType')
drop type TestTableType
create type TestTableType as table([index] int,[sub_index] int, [data] nvarchar(max))
go
--this table contains your input data
declare @tem table([index] int, [date] datetime , [sub_index] int, [angle] decimal (19,6),[length] decimal (19,6))
--temp table used to calculate maximum number of columns to show
declare @counters table([index] int,[sub_index] int, [counter] int)
--temp table that will hold denormalized values (one row for each index/subindex couple)
declare @denormalized_data TestTableType
--this variables contains the maximum number of columns to show
declare @max_columns int
--this variable will contain the dunamically generated TSQL query that will give the final result
declare @dynamic_query nvarchar(max)
--support variables used to generate dynamic query
declare @counter int
declare @counter_str nvarchar(max)
--1. populate input data
insert into @tem select 681 , '20161019 15:49', 1 , 30, 333
insert into @tem select 681 , '20161019 15:49', 1 , 60, 666
insert into @tem select 682 , '20161019 15:52', 2 , 0, 44
insert into @tem select 682 , '20161019 15:52', 2 , 45, 77
insert into @tem select 682 , '20161019 15:52', 2 , 90, 67
--insert into @tem select 682 , '20161019 15:52', 2 , 8, 88
--2. calculate the number of columns to show
insert into @counters
select [index],[sub_index], COUNT(*) from @tem group by [index],[sub_index]
select @max_columns = max([counter]) from @counters
--3. denormalize data using an XML-based approach to obtain one row for each index/subindex couple
insert into @denormalized_data
SELECT
[index],[sub_index],
'<MyData>'+ STUFF((
SELECT ' <angle>' + CAST([angle] AS VARCHAR(MAX)) + '</angle><length>' + CAST([length] AS VARCHAR(MAX)) +'</length>'
FROM @tem
WHERE ([index] = Results.[index] and [sub_index] = Results.[sub_index])
FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
,1,2,'') + '</MyData>' AS NameValues
FROM @tem Results
GROUP BY [index],[sub_index]
--4. generate a dynamic TSQL query with the correct number of columns
set @counter = 0
set @dynamic_query=' SELECT [index],[sub_index], '
while @counter < @max_columns
begin
set @counter = @counter +1
set @counter_str = CAST(@counter as nvarchar(max))
set @dynamic_query = @dynamic_query + ' CONVERT(XML,[data]).value(''/MyData[1]/angle['+ @counter_str + ']'',''varchar(100)'') AS angle'+ @counter_str + ', '
set @dynamic_query = @dynamic_query + ' CONVERT(XML,[data]).value(''/MyData[1]/length['+ @counter_str + ']'',''varchar(100)'') AS length'+ @counter_str + ', '
end
set @dynamic_query = substring(@dynamic_query,1,LEN(@dynamic_query) - 1) + ' FROM @denormalized_d '
exec sp_executesql @dynamic_query, N'@denormalized_d TestTableType readonly', @denormalized_data
ここでは3つの異なる値を持つ出力されます。ここでは
![enter image description here](https://i.stack.imgur.com/4sm25.png)
は、4つの異なる値(で出力されます行--insert into @tem select 682 , '20161019 15:52', 2 , 8, 88
のコメントを外してください):
![enter image description here](https://i.stack.imgur.com/CIUlk.png)
あなたが達成しようとしたことは、私たちに教えてください??希望の結果を得るためにピボットを使用してください –
まだ何か試しましたか?ピボットクエリと呼ばれるものを探しています。しかし、角度の数が可変であれば、この問題を処理するには動的SQLが必要になることがあります。 –
'sub_index'カラムが2番目のテーブルに混乱しているのがわかりました。あなたのために? – harshavmb