You need to use Dynamic SQL to construct all the distinct possible values to be used in PIVOT
In your case you can fetch the TOP 3 values and construct the column list and use PIVOT.
This Dynamic Column PIVOT article explains how to do it.
In the first select query, we are getting data values for the top 3 myid values and constructing the column list
Using this @cols variable inside the dynamic PIVOT
query.
You need to add order by on appropriate column.
DECLARE @cols NVARCHAR(2000)
SELECT @cols = STUFF(( SELECT DISTINCT
'],[' + t1.data
FROM Table1 AS t1
JOIN ( Select TOP 3 myId from Table1) T
on t1.myId = T.myId
ORDER BY '],[' + t1.data
FOR XML PATH('')
), 1, 2, '') + ']'
DECLARE @query NVARCHAR(4000)
SET @query = N'SELECT myId, '+
@cols +'
FROM
(SELECT t1.myId , t1.data
FROM Table1
JOIN ( Select TOP 3 myId from Table1) T
on t1.myId = T.myId
) p
PIVOT
(
MAX(myId)
FOR ColName IN
( '+
@cols +' )
) AS pvt
'
EXECUTE(@query)