I have a table 'propertyvalues' as follows:
ID FileID Property Value
1 x Name 1.pdf
2 x Size 12567
3 x Type application/pdf
4 y Name 2.pdf
5 y Size 23576
6 y Type application/pdf
......
and so on
How to write a SQL query on the table above to fetch a result like below
FileID Name Size Type
x 1.pdf 12567 application/pdf
y 2.pdf 23576 application/pdf
You did not specify RDBMS, if you know the number of columns to transform then you can hard-code the values:
This is basically a
PIVOT
function, some RDBMS will have aPIVOT
, if you do then you can use the following,PIVOT
is available in SQL Server, Oracle: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:A version with joins that works regardless of missing rows:
If you have a table where
FileID
is the primary key, you may replace theDISTINCT
subquery with that table.Regarding efficiency, it depends on a lot of factors. Examples:
Do all FileIDs have rows with Name, Size and Type and no other properties (and your table has a clustered index on
(FileID, Property)
)? Then theMAX(CASE...)
version would perform quite well as the whole table would have to be scanned anyway.Are there (many) more than 3 properties and a lot of FileIDs have no Name, Size and Type, then the
JOIN
version would work well with an index on(Property, FileID) INCLUDE (Value)
as only this index data would be used for the joins.Not sure how efficient is the
PIVOT
version.What I suggest though is to test the various versions with your data and table sizes, in your envirorment (version, disk, memory, settings, ...) before you select which one to use.