इसे आजमाएं
CREATE TABLE #table1
(
sourceid INT,
name VARCHAR(50),
value VARCHAR(50),
timestamp NUMERIC(10, 2)
)
INSERT INTO #table1
VALUES (1,
'Mark',
'99%',
9.00),
(1,
'Mark',
'75%',
9.30),
(1,
'Mark',
'60%',
10.00),
(1,
'Mark',
'85%',
10.30),
(2,
'John',
'50%',
9.00),
(2,
'John',
'30%',
9.30),
(2,
'John',
'65%',
10.00),
(2,
'John',
'79%',
10.30)
DECLARE @columns NVARCHAR(MAX),
@sql NVARCHAR(MAX);
SET @columns = N'';
SELECT @columns += N', p.' + QUOTENAME(timestamp)
FROM (SELECT DISTINCT p.timestamp
FROM #table1 AS p) AS x;
SET @sql = N'
select *from (SELECT *
FROM
(
SELECT * FROM #table1
) AS j
PIVOT
(
MAX(VALUE) FOR TIMESTAMP IN ('
+ STUFF(REPLACE(@columns, ', p.[', ',['), 1, 1, '')
+ ')
) AS p)tb
order by tb.sourceid';
EXEC sp_executesql
@sql;
DROP TABLE #table1