PostgreSQL - Numeric Functions
PostgreSQL - Modern Numeric Functions
Section titled “PostgreSQL - Modern Numeric Functions”PostgreSQL provides a rich library of mathematical and numeric functions essential for data analysis, calculations, and manipulation. This tutorial organizes these functions into logical groups and demonstrates their use with practical, modern examples.
Let’s use a sample products table for our examples:
CREATE TABLE products ( id SERIAL PRIMARY KEY, name TEXT, price NUMERIC(10, 2), stock_change INT, rating NUMERIC(3, 2));
INSERT INTO products (name, price, stock_change, rating) VALUES('Laptop', 1200.55, -10, 4.58),('Mouse', 25.00, 50, 4.20),('Keyboard', 75.99, 25, 4.66),('Webcam', 49.49, -5, 3.81);Rounding and Truncation Functions
Section titled “Rounding and Truncation Functions”These functions are used to adjust the precision of numeric values.
ROUND(n, d): Rounds a numberntoddecimal places. Ifdis omitted, rounds to the nearest integer.CEIL(n)orCEILING(n): Rounds a numbernUP to the nearest integer.FLOOR(n): Rounds a numbernDOWN to the nearest integer.
SELECT name, price, ROUND(price) AS rounded_to_integer, ROUND(price, 1) AS rounded_to_one_decimal, CEIL(price) AS ceiling_price, FLOOR(price) AS floor_priceFROM products WHERE name = 'Laptop';Result:
name | price | rounded_to_integer | rounded_to_one_decimal | ceiling_price | floor_price--------+---------+--------------------+------------------------+---------------+------------- Laptop | 1200.55 | 1201 | 1200.6 | 1201 | 1200Core Arithmetic Functions
Section titled “Core Arithmetic Functions”ABS(n): Returns the absolute (non-negative) value ofn.MOD(a, b): Returns the remainder ofadivided byb(modulo operator).SQRT(n): Returns the square root ofn.POW(base, exp)orPOWER(base, exp): Returnsbaseraised to the power ofexp.
-- Get the magnitude of stock changesSELECT name, stock_change, ABS(stock_change) AS absolute_change FROM products;
-- Example of POWERSELECT POWER(2, 8); -- Result: 256
-- Example of SQRTSELECT SQRT(64); -- Result: 8Value Comparison Functions
Section titled “Value Comparison Functions”These functions are useful for finding the extreme values from a list of arguments.
GREATEST(val1, val2, ...): Returns the largest value from the input list.LEAST(val1, val2, ...): Returns the smallest value from the input list.
-- Imagine a scenario with a base price and a sale price.SELECT name, price AS original_price, LEAST(price * 0.8, 1000.00) AS sale_price -- Sale is 20% off, capped at $1000FROM products WHERE name = 'Laptop';Result:
name | original_price | sale_price--------+----------------+------------ Laptop | 1200.55 | 960.44Trigonometric and Logarithmic Functions
Section titled “Trigonometric and Logarithmic Functions”PostgreSQL provides a full suite of functions for specialized scientific and engineering calculations. Note that trigonometric functions operate on radians.
Trigonometric Functions
Section titled “Trigonometric Functions”SIN(x),COS(x),TAN(x)ASIN(x),ACOS(x),ATAN(x),ATAN2(y, x)COT(x)RADIANS(degrees): Converts degrees to radians.DEGREES(radians): Converts radians to degrees.
-- Calculate the sine of 90 degreesSELECT SIN(RADIANS(90)); -- Result: 1Logarithmic Functions
Section titled “Logarithmic Functions”LOG(n): Natural logarithm (base e).LOG(b, n): Logarithm ofnwith baseb.EXP(n): Exponential function (e^n).
SELECT LOG(10, 100); -- Result: 2Other Useful Functions
Section titled “Other Useful Functions”PI(): Returns the constant value of Pi (π).SIGN(n): Returns -1 ifnis negative, 0 ifnis zero, and 1 ifnis positive.
SELECT name, stock_change, SIGN(stock_change) AS direction FROM products;
-- Result:-- name | stock_change | direction-- -----------+--------------+------------- Laptop | -10 | -1-- Mouse | 50 | 1-- Keyboard | 25 | 1-- Webcam | -5 | -1