You did not specify RDBMS, if you know the number of columns to transform then you can hard-code the values:
select FileId,
max(case when property = 'Name' then value end) Name,
max(case when property = 'Size' then value end) Size,
max(case when property = 'Type' then value end) Type
from yourtable
group by FileId
This is basically a PIVOT
function, some RDBMS will have a PIVOT
, if you do then you can use the following, PIVOT
is available in SQL Server, Oracle:
select *
from
(
select FileId, Property, Value
from yourTable
) x
pivot
(
max(value)
for property in ([Name], [Size], [Type])
) p
If you have an unknown number of columns to transform, then you can use a dynamic PIVOT
. This gets the list of columns to transform at run-time:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(property)
from yourtable
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT ' + @cols + ' from
(
select FileId, Property, Value
from yourtable
) x
pivot
(
max(value)
for Property in (' + @cols + ')
) p '
execute(@query)