简体   繁体   English

如何从连接表中选择数据作为表类型?

[英]How to select data from joined table as a table type?

I'd like to build de-normalised view that handles reference data in table type. 我想构建处理表类型中的引用数据的非规范化视图。

create table reftab (id number, name varchar2(40), details varchar2(1000));
/
create table basictab (id number, name varchar2(300), contract varchar2 (20));
/
create or replace type reftype is object (id number, name varchar2(40), details varchar2(1000));
/
create or replace type reftypetab as table of reftype;
/
insert into basictab values (1, 'aaa', 'c1');
insert into basictab values (2, 'aab', 'c1');
insert into basictab values (3, 'aaa', 'c2');
insert into basictab values (4, 'aaa', 'c3');

insert into reftab values (1, 'asd', 'aaa');
insert into reftab values (1, 'asg', 'ass');
insert into reftab values (1, 'ash', 'add');
insert into reftab values (1, 'asf', 'agg');
insert into reftab values (3, 'asd', 'aaa');
insert into reftab values (3, 'ad', 'aa');
insert into reftab values (4, 'asd', 'aaa');
insert into reftab values (4, 'as', 'a');
insert into  values (4, 'ad', 'aa');
/

With such data I'd like to have view that contains 4 rows of basictab with additional column that is reftypetab and contains all ref data joined on id . 与这样的数据,我想有观点,即包含4排basictab与附加列,它是reftypetab并包含所有参考文献数据上接合id

I know I can obtain it by: 我知道我可以通过以下方式获得它:

CREATE OR REPLACE FUNCTION pipef (p_id IN NUMBER) RETURN reftypetab PIPELINED AS
BEGIN
  FOR x IN (select * from reftab where id = p_id) LOOP
    PIPE ROW(reftype(x.id, x.name, x.details));   
  END LOOP;

  RETURN;
END;
/

SELECT id, pipef(id)
FROM  reftab
group BY id;
/

but is there any better way without function to get the result? 但有没有更好的方法没有功能来得到结果?

Your current set-up gets: 您当前的设置得到:

SELECT id, pipef(id) as result
FROM  reftab
group BY id;

        ID RESULT(ID, NAME, DETAILS)                                                                                               
---------- ------------------------------------------------------------------------------------------------------------------------
         1 REFTYPETAB(REFTYPE(1, 'asd', 'aaa'), REFTYPE(1, 'asg', 'ass'), REFTYPE(1, 'ash', 'add'), REFTYPE(1, 'asf', 'agg'))      
         4 REFTYPETAB(REFTYPE(4, 'asd', 'aaa'), REFTYPE(4, 'as', 'a'), REFTYPE(4, 'ad', 'aa'))                                     
         3 REFTYPETAB(REFTYPE(3, 'asd', 'aaa'), REFTYPE(3, 'ad', 'aa'))                                                            

You could use the collect() function to simplify that: 您可以使用collect()函数来简化:

select id, cast(collect(reftype(id, name, details)) as reftypetab) as result
from reftab
group by id;

        ID RESULT(ID, NAME, DETAILS)                                                                                               
---------- ------------------------------------------------------------------------------------------------------------------------
         1 REFTYPETAB(REFTYPE(1, 'asd', 'aaa'), REFTYPE(1, 'asf', 'agg'), REFTYPE(1, 'ash', 'add'), REFTYPE(1, 'asg', 'ass'))      
         3 REFTYPETAB(REFTYPE(3, 'asd', 'aaa'), REFTYPE(3, 'ad', 'aa'))                                                            
         4 REFTYPETAB(REFTYPE(4, 'asd', 'aaa'), REFTYPE(4, 'ad', 'aa'), REFTYPE(4, 'as', 'a'))                                     

If you want information from basictab as well you can use a multiset operator : 如果您还需要来自basictab信息,可以使用multiset运算符

select bt.id, bt.name,
  cast(multiset(select reftype(rt.id, rt.name, rt.details)
    from reftab rt where rt.id = bt.id) as reftypetab) as result
from basictab bt;

        ID NAME       RESULT(ID, NAME, DETAILS)                                                                                               
---------- ---------- ------------------------------------------------------------------------------------------------------------------------
         1 aaa        REFTYPETAB(REFTYPE(1, 'asd', 'aaa'), REFTYPE(1, 'asg', 'ass'), REFTYPE(1, 'ash', 'add'), REFTYPE(1, 'asf', 'agg'))      
         2 aab        REFTYPETAB()                                                                                                            
         3 aaa        REFTYPETAB(REFTYPE(3, 'asd', 'aaa'), REFTYPE(3, 'ad', 'aa'))                                                            
         4 aaa        REFTYPETAB(REFTYPE(4, 'asd', 'aaa'), REFTYPE(4, 'as', 'a'), REFTYPE(4, 'ad', 'aa'))                                     

Further research gave me another way to do that: 进一步的研究给了我另一种方法:

SELECT id, CAST(COLLECT(reftype(r.id, r.name, r.details)) AS reftypetab) AS customer_ids
    FROM reftab r group by id;

This looks much better but still I would ask if there are any opther ways to do that. 这看起来好多了,但我仍然会问,是否有任何其他方法可以做到这一点。

EDIT 编辑

Maybe that's not exactly what I was asking about but cursor expresion can give similar result. 也许这不是我所询问的,但是光标表达可以给出类似的结果。

 SELECT id, cursor(select reftype(r.id, r.name, r.details) from reftab r where r.id = b.id)  AS customer_ids
    FROM basictab b; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM