Search code examples
sqloracle-database

data type changes when using AVG in view definition


I have a table with a column of FLOAT data type. Based on this table, I create a view which returns AVG of that column.

create table my_tab (oo float);
create or replace view my_view (oo) as select avg(oo) from my_tab;

describing the view

desc my_view;

will return

Name Null? Type


OO NUMBER

If this an Oracle bug? Can I force somehow to maintain the source data type?

Thank you,


Solution

  • It is not necessarily a bug as it tries to be more precise using Number.

    In the documentation it says :

    A subtype of the NUMBER data type having precision p. A FLOAT value is represented internally as NUMBER.

    Few other supporting docs.

    You can still cast it to float as below :

    create or replace view my_view (oo) as
    select cast(avg(oo) as float) from my_tab;
    

    Fiddle