Oracle PL / SQL Denomalised结果

时间:2009-11-10 11:24:19

标签: sql oracle stored-procedures plsql

给出三个表:汽车表,临时表和链接表,如:

table_car
---------
int car_id
string make
string model

table_extras
------------
int extra_id
string extra

table_car_extras_link
---------------------
int car_id
int extra_id

我想编写一个PL / SQL存储过程,它以下列方式返回数据:

car_id, make, model, extra[]

e.g。

1, Ford, Fiesta, sunroof;electric windows
2, BMW, M3, sports pack;alarm;sat nav
3, subary, impreza, leather seats;ABS

对于数据库我是一个非常新手,所以任何帮助都会受到赞赏。请注意,在我们的真实系统中,我们将返回1000辆“汽车”,每辆汽车最多可增加10个'额外'

2 个答案:

答案 0 :(得分:4)

这应该只是一个视图,不需要程序:

create view myview as
select c.car_id, c.make, c.model, WM_CONCAT(e.extra) as extras
from table_car c, table_car_extras_link l, table_extras e
where c.car_id=l.car_id and l.extra_id=e.extra_id
group by c.car_id;

WM_CONCAT就像字符串的SUM一样。

有关连接技术,请参阅this page

答案 1 :(得分:2)

以下内容适用于9i及以上版本,它使用Tom Kyte's concatenation function

SELECT c.car_id, c.make, c.model, stragg(e.extra)
  FROM table_car c
  LEFT JOIN table_car_extras_link ce ON c.car_id = ce.car_id
  LEFT JOIN table_extras e ON ce.extra_id = e.extra_id
GROUP BY c.car_id, c.make, c.model