2

I have a table in PostgreSQL defined as:

CREATE TABLE ITEM_PROPERTIES
( 
  ITEM_ID NUMERIC(11,0),
  ITEM_DIMENSIONAL_UOM VARCHAR(30),
  ITEM_HEIGHT NUMERIC(38,14),
  ITEM_WIDTH NUMERIC(38,14),
  ITEM_LENGTH NUMERIC(38,14),
  ITEM_WEIGHT_UOM VARCHAR(30),
  ITEM_WEIGHT NUMERIC(38,14),
  ITEM_CUBE NUMERIC(38,14)
);

The length/width/height of the items are stored in the table and I need to calculate the cube via the following SQL:

update ITEM_PROPERTIES set ITEM_CUBE = ITEM_WIDTH*ITEM_HEIGHT*ITEM_LENGTH
where ITEM_WIDTH<10000 and ITEM_HEIGHT < 10000 and ITEM_LENGTH<10000;

Then I encountered precision overflow:

ERROR: Resulting scale overflows maximum precision

How to solve this?

3 Answers 3

2

Finally, this SQL works:

update ITEM_PROPERTIES set ITEM_CUBE = ITEM_WIDTH::decimal(12,3)*ITEM_HEIGHT::decimal(12,3)*ITEM_LENGTH::decimal(12,3)
where ITEM_WIDTH<10000 and ITEM_HEIGHT < 10000 and ITEM_LENGTH<10000;
Sign up to request clarification or add additional context in comments.

Comments

0

Use truncation maybe?

update ITEM_PROPERTIES set ITEM_CUBE = trunc(ITEM_WIDTH*ITEM_HEIGHT*ITEM_LENGTH, 14)
where
     ITEM_WIDTH<10000
     and ITEM_HEIGHT < 10000
     and ITEM_LENGTH<10000;

Comments

0

You could try rounding it or something to that order.

But the real problem stems from your types. If length, width and height are numeric(38,14), the volume should be more like numeric(114,42). And in fact a bit more, to accommodate for carries in the cases where the scale and precision are both used up in the three dimensions.

IMHO, get rid of the precision and scale entirely: store the numbers as numeric instead of numeric(38,14), and avoid that the problem might arise in the first place. (If you really need 14 decimals, round it in your application or using a before trigger.)

1 Comment

That too, lol. :-) It's convenient for caching when indexes on expressions aren't available, that being said.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.