2016-09-24 45 views
0

我想將行轉換爲列(如PIVOT),並且如果值的數量增加,我無法獲取。將行轉換爲列(如PIVOT)如果在SQL Server中增加值的數量不起作用

下面是我的表格。

Table1

我想這樣的輸出。

Table2

我用下面的查詢,以達致這,但沒有運氣。

查詢1

Create table #temp(instanceid int, submissionid int, name1 varchar(20), value1 varchar(20)) 

insert into #temp(instanceid,submissionid,name1,value1) 
Select 5151,5532,'Question_1','Y' 
union 
Select 5151,5532,'First','Mujda' 
union 
Select 5151,5532,'Last','Zhublawar' 
union 
Select 5151,5532,'Question_1','Y' 
union 
Select 5151,5532,'First','Mujda1' 
union 
Select 5151,5532,'Last','Zhublawar1' 
union 
Select 5151,5532,'Question_1','Y' 
union 
Select 5152,5533,'First','Muthu' 
union 
Select 5151,5533,'Last','Kumar' 
union 
Select 5152,5533,'Question_1','Y' 
union 
Select 5152,5533,'First','Muthu1' 
union 
Select 5152,5533,'Last','Kumar1' 


GO 

DECLARE @SQLQuery AS NVARCHAR(MAX) 
DECLARE @PivotColumns AS NVARCHAR(MAX) 
DECLARE @PivotValues AS NVARCHAR(MAX) 

--Get unique values of pivot column 
SELECT @PivotColumns= COALESCE(@PivotColumns + ',','') + QUOTENAME(seq) 
FROM (
select (cast(row_number() over(partition by name1 order by name1) as varchar(10)) + name1) as seq 
from #temp group by value1,name1,instanceid 
) AS PivotExample 


--Create the dynamic query with all the values for 
--pivot column at runtime 
SET @SQLQuery = 
    N'SELECT instanceid,submissionid, ' + @PivotColumns + ' 
    FROM #temp 
    PIVOT(MAX(value1) 
      FOR name1 IN (' + @PivotColumns + ')) AS P' 

--Execute dynamic query 
EXEC sp_executesql @SQLQuery 

DROP TABLE #temp 

QUERY2:

Create table #temp(instanceid int, submissionid int, name1 varchar(20), value1 varchar(20)) 

insert into #temp(instanceid,submissionid,name1,value1) 
Select 5151,5532,'Question_1','Y' 
union 
Select 5151,5532,'First','Mujda' 
union 
Select 5151,5532,'Last','Zhublawar' 
union 
Select 5151,5532,'Interest','100' 

select * from(
Select 
instanceid, 
submissionid, 
[1st Ownership First Name] = Case when name1='First' then value1 end, 
[1st Ownership Last Name] = Case when name1='Last' then value1 end, 
[1st Ownership Question] = Case when name1='Question_1' then value1 end 
from #temp 
group by instanceid,submissionid,name1,value1 
) P where p.[1st Ownership First Name] is not null or p.[1st Ownership Last Name] is not null or p.[1st Ownership Question] is not null 


drop table #temp 

回答

1

您需要一個額外的字段來獲取該數據中的所有副本的某些順序。
例如一個主鍵。

然後,您可以使用帶有按順序使用該額外字段的row_number的數據透視表。
然後將row_number與name1連接起來,然後對這些連接進行旋轉。

例如:

create table #temp (id int identity(1,1), instanceid int, submissionid int, name1 varchar(20), value1 varchar(20)); 

insert into #temp(instanceid,submissionid,name1,value1) values 
(5151,5532,'First','Mujda'), 
(5151,5532,'Last','Zhublawar'), 
(5151,5532,'Question_1','Y'), 
(5151,5532,'First','Mujda1'), 
(5151,5532,'Last','Zhublawar1'), 
(5151,5532,'Question_1','Y'), 
(5151,5532,'First','Mujda1'), 
(5151,5532,'Last','Zhublawar1'), 
(5151,5532,'Question_1','Y'), 
(5151,5533,'First','Muthu'), 
(5151,5533,'Last','Kumar'), 
(5151,5533,'Question_1','Y'), 
(5151,5534,'First','Suresh'), 
(5151,5534,'Last','Kumar'), 
(5151,5534,'Question_1','Y'), 
(5151,5534,'First','Suresh1'), 
(5151,5534,'Last','Kumar1'), 
(5151,5534,'Question_1','Y'); 

SELECT 
instanceid, 
submissionid, 
[First1] as [1st First], [Last1] as [1st Last], [Question_11] as [1st Question_1], 
[First2] as [2nd First], [Last2] as [2nd Last], [Question_12] as [2nd Question_1], 
[First3] as [3rd First], [Last3] as [3rd Last], [Question_13] as [3rd Question_1] 
FROM (
    select 
    instanceid, 
    submissionid, 
    concat(name1, row_number() over (partition by instanceid, submissionid, name1 order by id)) as name_rn, value1 
    from #temp 
    where name1 in ('First','Last','Question_1') 
    ) t 
PIVOT(MAX(value1) 
FOR name_rn IN (
    [First1], [Last1], [Question_11], 
    [First2], [Last2], [Question_12], 
    [First3], [Last3], [Question_13] 
    ) 
) AS Pvt; 

要做到這一點的動態的方式,這裏的一些SQL生成一個@SQL變量。

declare @T table (name1 varchar(20)); 

insert into @T select name1 from #temp group by name1 order by name1; 

declare @SQL nvarchar(max); 
declare @Fields1 nvarchar(max); 
declare @Fields2 nvarchar(max); 

SELECT @Fields2 = STUFF((select ', ' + quotename(name1) from @T order by name1 FOR XML PATH('')),1,1,''); 
SET @Fields2 = replace(@Fields2,']','1]')+','+char(13)+replace(@Fields2,']','2]')+','+char(13)+replace(@Fields2,']','3]'); 

SELECT @Fields1 = STUFF((select ',$' + quotename(name1+n)+' as '+quotename(nx+' '+name1) from (
    select '1' as n, '1st' as nx, name1 from @T union all 
    select '2', '2nd' as nx, name1 from @T union all 
    select '3', '3rd' as nx, name1 from @T 
    ) q order by n, name1 FOR XML PATH('')),1,1,''); 
SET @Fields1 = replace(@Fields1,'$',char(13)); 

SET @SQL = 
'SELECT 
instanceid, 
submissionid,'[email protected]+' 
FROM (
    select 
    instanceid, 
    submissionid, 
    concat(name1, row_number() over (partition by instanceid, submissionid, name1 order by id)) as name_rn, value1 
    from #temp 
    ) t 
PIVOT(MAX(value1) 
FOR name_rn IN ('+char(13)[email protected]+') 
) AS Pvt'; 

select @SQL; 
+0

真棒!它按預期工作..謝謝:) –

0
--Get unique values of pivot column 
SELECT @PivotColumns= COALESCE(@PivotColumns + ',','') + QUOTENAME(seq) 
FROM (
select DISTINCT (name1) as seq 
from #temp group by value1,name1,instanceid 
) AS PivotExample 

這都是你的問題,你是一個串聯ROW_NUMBER到NAME1值將始終爲null。你的列表需要是name1中的DISTINCT值,而不是你希望他們最終得到的值。如果您想重命名列,您可以在透視後對列進行別名,或者在運行透視之前更改name1中的值。

因此在上面的代碼中,我刪除了ROW_NUMBER()並將DISTINCT添加到派生表中。

另請注意,您所包含的測試數據有一些實例ID & submissionid組合缺少某些字段,比如名字或問題。它看起來像複製和粘貼你只是沒有糾正相關性。