我試圖在select語句中分兩列,然後將商四捨五入到小數點後4位。舍入一個分數的商數?
select round(round(sum(case when acct_no = '2999'
and date between '1/1/14' and current_date then amount end)::numeric, 4)::float
/round(sum(case when acct_no = '3989'
and date between '1/1/14' and current_date then amount end)::numeric, 4)::numeric, 4) column
from table
查詢的其餘部分將保存多個日期,所以在那裏的日期應該是必要的。
它給錯誤:
ERROR: function round(double precision, integer) does not exist
這是試圖在PostgreSQL中。
看起來應該可以工作!你是否嘗試投射而不是:: numeric,如:cast(... as numeric)?此外,你可以嘗試將'amount'合併爲0或某些東西(以防其返回null)(例如coalesce(amount,0 :: expectedtype)) – Greg
截至目前它正在工作,但唯一的問題是它不是四捨五入。小數點後仍有14位數字。 – Padagomez
這是因爲這輪比賽需要在師後進行。而不是個別的分子和分母。'round(round(sum(case when acct_no ='2999'and date between'1/1/14'and current_date then amount end):: numeric,4):: float /round(sum(case when acct_no =' 3989' 和'1/1/14'和current_date之間的日期,然後數量結束)::數字,4)::數字,4)'我認爲從本質上講,你說22.0000到4,然後是7.000到4,然後將兩個給... 3.1428571 ...但不要繞它。 – xQbert