Re: simple division - Mailing list pgsql-general

From Thomas Kellerer
Subject Re: simple division
Date
Msg-id 1cbf43df-b495-f5f2-fcfd-b610a279e17b@gmx.net
Whole thread Raw
In response to Re: simple division  (Martin Mueller <martinmueller@northwestern.edu>)
List pgsql-general
Martin Mueller schrieb am 04.12.2018 um 21:57:
> I didn't formulate my question properly, because the query went like
>   "select alldefects /wordcount"
> where alldefects and wordcount are integers.   
> But none of the different ways of putting the double colon seemed to
> work.
One way is to make one of the integers a decimal by multiplying with 1.0

    select alldefects * 1.0 / wordcount

> The Postgres notation of this simple procedure is very unintuitive. I
> haven't been able to remember several times, and most people think of
> me as a person with a reasonably good memory.

Postgres supports the SQL standard's CAST operator:

    select cast(alldefects as decimal) / wordcount

The "Postgres way" would be:

    select alldefects::decimal / wordcount

> There is no obvious place in the documentation to look this up.

This is covered in the chapter "Type Casts"

https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-TYPE-CASTS




pgsql-general by date:

Previous
From: Martin Mueller
Date:
Subject: Re: simple division
Next
From: Rob Sargent
Date:
Subject: Re: simple division