Oracle SQL — Grabbing values from multiple rows

时间秒杀一切 提交于 2019-12-02 03:32:29

You can pivot the data like this in any version of Oracle.

SELECT id,
       MAX( CASE WHEN key = 'name' THEN value ELSE null END ) name,
       MAX( CASE WHEN key = 'height' THEN value ELSE null END ) height,
       MAX( CASE WHEN key = 'awesomeness' THEN value ELSE null END ) awesomeness
  FROM facts
 WHERE id IN (1,2,3)
 GROUP BY id

If you're using 11g, you could also use the PVOT operator.

If this is representative of your data model, though, that sort of entity-attribute data model is generally going to be rather inefficient. You would generally be much better served with a table that had columns for name, height, awesomeness, etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!