您好我有一个SQL查询,其结果如下表
ID NAME problem_ID date_of_entry elem_id staff_id 1 abc 456 12/12/2014 789 32 1 abc 768 12/01/2014 896 67 1 abc 897 02/14/2014 875 98 2 bcd 723 02/17/2014 287 09 2 bcd 923 09/13/2014 879 01 2 bcd 878 08/23/2014 hgd 34
我想要结果如下
ID NAME problem_ID_1 problem_ID_2 problem_ID_3 date_of_entry_1 date_of_entry_2 date_of_entry_3 elem_id_1 elem_id_2 elem_id_3 staff_id_1 staff_id_2 staff_id_3
problem_id,date_of条目,elem_id,staff_id都是动态的。你能告诉我如何使用枢轴功能或任何其他方式做到这一点。
答案 0 :(得分:0)
试试这个
我已完成两个列problem_ID and date_of_entry
您可以在pivot中添加另外两列。
这里有一个虚拟演示 http://sqlfiddle.com/#!3/ef8e8e/1
CREATE TABLE #Products
(
ID INT,
NAME VARCHAR(30),
problem_ID INT,
date_of_entry DATE,
elem_id VARCHAR(30),
staff_id INT
);
INSERT INTO #Products
VALUES (1,'abc',456,'2014/12/12',789,32),
(1,'abc',768,'2014/12/01',896,67),
(1,'abc',897,'2014/02/14',875,98),
(2,'bcd',723,'2014/02/17',287,09),
(2,'bcd',923,'2014/09/13',879,01),
(2,'bcd',878,'2014/08/23','hgd',34)
DECLARE @problm VARCHAR(MAX)='',
@daofenty_id VARCHAR(MAX)='',
@aggproblm VARCHAR(MAX)='',
@aggdaofenty_id VARCHAR(MAX)='',
@sql NVARCHAR(max)
SET @problm = (SELECT DISTINCT Quotename('problm'+CONVERT(VARCHAR(50), Row_number() OVER(partition BY ID ORDER BY problem_ID)))
+ ','
FROM #Products
FOR XML PATH(''))
SET @aggproblm = (SELECT DISTINCT ' max('
+ Quotename('problm'+CONVERT(VARCHAR(50), Row_number() OVER(partition BY ID ORDER BY problem_ID)))
+ ') problm'
+ CONVERT(VARCHAR(50), Row_number() OVER(partition BY ID ORDER BY problem_ID))
+ ','
FROM #Products
FOR XML PATH(''))
SET @daofenty_id =(SELECT DISTINCT
+ Quotename('daofenty_id'+CONVERT(VARCHAR(50), Row_number() OVER(partition BY ID ORDER BY date_of_entry)))
+ ','
FROM #Products
FOR XML PATH(''))
SET @aggdaofenty_id = (SELECT DISTINCT + ' max('
+ Quotename('daofenty_id'+CONVERT(VARCHAR(50), Row_number() OVER(partition BY ID ORDER BY date_of_entry)))
+ ') daofenty_id'
+ CONVERT(VARCHAR(50), Row_number() OVER(partition BY ID ORDER BY date_of_entry))
+ ','
FROM #Products
FOR XML PATH(''))
SET @problm = LEFT(@problm, Len(@problm) - 1)
SET @daofenty_id = LEFT(@daofenty_id, Len(@daofenty_id) - 1)
SET @aggproblm = LEFT(@aggproblm, Len(@aggproblm) - 1)
SET @aggdaofenty_id = LEFT(@aggdaofenty_id, Len(@aggdaofenty_id) - 1)
SET @sql = 'SELECT Id,name,' + @aggproblm + ','
+ @aggdaofenty_id + '
FROM (select * from (SELECT ''problm''+convert(varchar(50),row_number() over(partition by ID order by problem_ID)) problm_id, ''daofenty_id''+convert(varchar(50),row_number() over(partition by ID order by date_of_entry)) daofenty_id ,
''elemid''+convert(varchar(50),row_number() over(partition by ID order by elem_id)) elemid , ''staffid''+convert(varchar(50),row_number() over(partition by ID order by staff_id)) staffid,*
FROM #Products) A
) AS T
PIVOT
(max(problem_id) FOR problm_id IN
(' + @problm + ')) AS P1
PIVOT
(max(date_of_entry) FOR daofenty_id IN
('
+ @daofenty_id + ')) AS P1
group by id,name'
--PRINT @sql
EXEC Sp_executesql
@sql
限制号码。列
SET @problm = (SELECT DISTINCT TOP N Quotename('problm'+CONVERT(VARCHAR(50), Row_number() OVER(partition BY ID ORDER BY problem_ID)))
+ ','
FROM #Products
FOR XML PATH(''))
同样地对其他列做同样的事情..