Search code examples
sqldatabasepostgresqlviewsqldatatypes

cannot change data type of view column from numeric to numeric(4,1)


I'm trying to calculate a ratio with 1 decimal place. So I created a view using

create or replace view MyView
as
select cast(A/B as numeric(4, 1)) as ratio from MyTable;

A is int type and B is real number with 3 decimal places and I'm getting an error

cannot change data type of view column "ratio" from numeric to numeric(4,1)

Any idea how to fix this?


Solution

  • drop first:

    drop view MyView;
    create or replace view MyView as
    select cast(A/B as numeric(4, 1)) as ratio 
    from MyTable;