我從XML生成一個臨時表的SQL服務器,它具有以下結構如何行轉換成列
column1 column2
id 1234
code ukjk
.
.
there will be another 60 rows again
id 4586
code ouiu
.
.
there will be another 60 rows again
id 78545
code khdsjf
like this there will be 15 id's in the table
現在我想將行轉換成列狀
我不不想全部60我只需要5人
id code vdb_id guid ....
1234
4586
78545
和我也有附加的XML文件圖像..
我已經使用旋轉功能
IF OBJECT_ID('tempdb..#xml_final') IS NOT NULL DROP TABLE #xml_final
CREATE TABLE #xml_final (rownum int, Project_id int, Project_Name varchar(60), Project_manager_id int, Updated_date varchar(60))
insert into #xml_final
SELECT * FROM
(
SELECT xmlData.Result.value('v[1]','VARCHAR(200)') AS A,
xmlData.Result.value('n[1]','VARCHAR(200)') AS B,
ROW_NUMBER() over(PARTITION BY xmlData.Result.value('n[1]','VARCHAR(200)')
order by xmlData.Result.value('v[1]','VARCHAR(200)')) AS Num
FROM @xmlData.nodes('//result/rows/row/f') xmlData(Result)
) AS A
Pivot (Min(A) FOR B IN ([id] ,
[summary],
[manager_id],
[ts]
)
) piv
但它不能正常工作..我越來越不匹配錯誤
例如
id 1234 i am getting code as ouiu
輸出2表將有3列
row_num key2 item_value
1 id 1234
5 proj_id 7655
9 task_name kfhkjfdhg
21 id 5455
25 proj_id 6789797
29 task_name dsyhuiyfhdi
和key2將只包含這3列名稱
輸出我想有是,
id proj_id task_name
1234 7655 kfhkjfdhg
5455 6789797 dsyhuiyfhdi
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(key2)
from #output_2
group by key2, row_num
order by row_num
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = N'SELECT ' + @cols + N' from
(
select item_value, key2
from #output_2
) x
pivot
(
max(item_value)
for key2 in (' + @cols + N')
) p '
print @query
exec sp_executesql @query;
更新查詢
DECLARE @columns VARCHAR(max)='',
@sql NVARCHAR(max)
SELECT @columns += COL
FROM (SELECT DISTINCT '[' + Isnull(key2, '') + '],' col
FROM #output_2
WHERE item_value IS NOT NULL) a
SELECT @columns = LEFT(@columns, Len(@columns) - 1)
SET @sql ='SELECT rownum,' + @columns + '
FROM (SELECT Row_number()OVER(PARTITION BY key2 ORDER BY item_value) rownum,
*
FROM #output_2)a
PIVOT(Max(item_value)
FOR key2 IN(' + @columns + ')) piv '
Print @sql
EXEC Sp_executesql @sql
更新問題2:
IF OBJECT_ID('tempdb..#output_3') IS NOT NULL DROP TABLE #output_3
CREATE TABLE #output_3 ( row_id int, row_num int , task_id int, project_id int, task_name varchar(200))
insert into #output_3
SELECT Row_number() over (ORDER BY (select null)) row_id , rownum,(id),(project_id),(summary)
FROM (SELECT Row_number()OVER(PARTITION BY key2 ORDER BY (select null)) rownum,
*
FROM #output_2)a
PIVOT(Max(item_value)
FOR key2 IN([id],[project_id],[summary])) piv
select row_id, task_id, task_name, project_id from #output_3
輸出:
插入查詢
IF OBJECT_ID('tempdb..#output') IS NOT NULL DROP TABLE #output
CREATE TABLE #output (id int ,key2 varchar(50), item_value varchar(200))
insert into #output
select
ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) as SNumber,
key2 = Item2.value('(n)[1]','varchar(50)'),
ItemValue = Item2.value('(v)[1]','varchar(50)')
from
@xmlData.nodes('/result/rows') as T(Item)
cross apply
item.nodes('row/f') as T2(Item2)
如何來解決這個問題..
how can i read multiple xml values from URL through t-sql
您的預期輸出不清楚! – 2014-12-13 03:03:13
@NoDisplayName:我已經添加了一個圖像,預期的輸出應該如何看起來像 – navbingo 2014-12-13 03:19:43
@NoDisplayName:我已經添加了一個鏈接,從那裏只有我陷害了tsql查詢xml輸入。 – navbingo 2014-12-13 03:23:22