How can I format a decimal (DOUBLE) number in a select statement?

0

Hi, I have the following select statement in Athena and I would like to set the number of digits right to the decimal period of the column value:

SELECT
,(CAST(column1 AS double) / column2) cvr
FROM
  my_table

For example, I current get the following result: 0.6453524

I would like to get it as 0.645

질문됨 2년 전13357회 조회
3개 답변
1

Hi, it depends on what specifically do you want, with a similar example 0.6456524 and 3 decimals

  • If you want to have a ceiling of the previous decimal value : 0.646 To do this you can use a UDF or this mathematic expression CEIL((CAST(column1 AS double) / column2)*1000)/1000 as cvr
  • if you want to have a floor of the previous decimal value : 0.645 FLOOR((CAST(column1 AS double) / column2)*1000)/1000 as cvr
  • if you want to numerically round the previous decimal value : 0.646. To do this you can use ROUND((CAST(column1 AS double) / column2), 3) as cvr You can always refer to the Athena operations documentation here
AWS
답변함 2년 전
AWS
전문가
검토됨 2년 전
0

Suggest to use the round function. See here

profile pictureAWS
전문가
Roi
답변함 2년 전
AWS
전문가
검토됨 2년 전
0

You can use ROUND(col, 3) to round a number to three decimal points:

SELECT ROUND(CAST(column1 AS DOUBLE) / column2), 3) AS cvr
FROM my_table
AWS
Theo
답변함 2년 전
AWS
전문가
검토됨 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠