【问题标题】:Oracle sql select data from table where attribute is nested tableOracle sql 从属性为嵌套表的表中选择数据
【发布时间】:2017-03-21 02:27:49
【问题描述】:

我有一个对象:

create type t_history_rec is object
(
  date_from date,
  current float
);

create type t_history is table of t_history_rec;

和表定义:

create table person
(
  id integer primary key,
  name varchar2(30),
  history t_history

);

我想像这样获取选择名称、history.date_from、history.current:

name1 date1 current1
name1 date2 current2
name2 date3 current3
...

如何做到这一点?

【问题讨论】:

    标签: oracle oracle11g nested-table


    【解决方案1】:

    无法验证这一点,但您可以尝试以下方法:

    select p.name, pp.date_from, pp.current 
    from person p, table(p.history) pp;
    

    【讨论】:

      【解决方案2】:

      你有一些错误。 current 已保留

      create or replace type t_history_rec is object
      (
        date_from date,
        curr float
      );
      /
      create type t_history is table of t_history_rec;
      /
      

      表定义需要store as

      create table person
      (
        id integer primary key,
        name varchar2(30),
        history t_history
      ) NESTED TABLE history STORE AS col1_tab;
      
      insert into person (id, name, history) values (1, 'aa', t_history(t_history_rec(sysdate, 1)));
      insert into person (id, name, history) values (2, 'aa', t_history(t_history_rec(sysdate, 1), t_history_rec(sysdate, 1)));
      

      然后选择是:

      SELECT t1.name, t2.date_from, t2.curr FROM person t1, TABLE(t1.history) t2;
      

      【讨论】:

        猜你喜欢
        • 2012-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-07
        • 1970-01-01
        相关资源
        最近更新 更多