Search code examples
sqloracle-databasefunctionuser-defined-types

Oracle SQL: using LAG function with user-defined-type returns "inconsistent datatypes"


I have a type MyType defined as follows:

create or replace type MyType as varray(20000) of number(18);

And a table MyTable defined as follows:

create table MyTable (
   id       number(18) primary key
  ,widgets  MyType
)

I am trying to select the widgets for each row and its logically previous row in MyTable using the following SQL:

select  t.id
       ,lag(t.widgets,1) over (order by t.id) as widgets_previous
from MyTable t
order by t.id;

and I get the response:

ORA-00932: inconsistent datatypes: expected - got MYSCHEMA.MYTYPE

If I run the exact same query using a column of type varchar or number instead of MyType it works fine.

The type of the column in the current row and its previous row must be the same so I can only assume it is something related to the user defined type.

Do I need to do something special to use LAG with a user defined type, or does LAG not support user defined types? If the latter, are there any other utility functions that would provide the same functionality or do I need to do a traditional self join in order to achieve the same?


Solution

  • After reading all the above I've opted for the following as the most effective method for achieving what I need:

    select curr.id
          ,curr.widgets  as widgets
          ,prev.widgets  as previous_widgets
    from (select  a.id                                      
                 ,a.widgets
                 ,lag(a.id,1) over (order by a.id)  as previous_id
          from mytable a
         ) curr
         left join mytable prev on (prev.id = curr.previous_id)
    order by curr.id
    

    ie. a lag / self join hybrid using lag on a number field that it doesn't complain about to identify the join condition. It's fairly tidy I think and I get my collections as desired. Thanks to everyone for the extremely useful input.