Oracle 11g database has 2 tables:
Items
ITEM_ID | NAME ================= 1 | first 2 | second and
ItemFields
ITEM_ID | FIELD_NAME | VALUE ===================================================== 1 | Field1 | aaa 1 | Field2 | bbb 2 | Field1 | ccc 2 | Field2 | ddd Each line from Items corresponds to several lines from ItemFields.
How to get a table view
ITEM_ID | NAME | Field1 | Field2 =================================== 1 | first | aaa | bbb 2 | second | ccc | ddd I see several options, but I would like to choose the most effective:
1) Use LEFT JOIN and LEFT JOIN through the data in the client application
2) Use the LEFT JOIN and loop through the result in the stored procedure, creating a new resultset
3) Use subqueries
SELECT i.ITEM_ID, i.NAME, (SELECT VALUE FROM ItemFields WHERE ITEM_ID=i.ITEM_ID AND FIELD_NAME='Field1') as Field1, (SELECT VALUE FROM ItemFields WHERE ITEM_ID=i.ITEM_ID AND FIELD_NAME='Field2') as Field2 FROM Items 4) Use PIVOT (to be honest, I didn’t quite understand how to work with it and whether it can work with several fields: VALUE_INT, VALUE_DATE)