DEV Community

Cover image for SQL Math Functions with Use Cases

SQL Math Functions with Use Cases

Xaman on November 28, 2023

In this context, ABS(x): Returns the absolute value of the input value 'x'. For example, ABS(-10) would return 10. ROUND(x, d): Rounds the input ...
Collapse
 
aarone4 profile image
Aaron Reese

If you need to include more information than just the grouped filed and aggregate you can also use the OVER() syntax.

SELECT category, SQRT(SUM(sales)) AS square_root_sales
FROM products
GROUP BY category;

becomes
SELECT category, ProductName, SQRT(SUM(sales) OVER (PARTITION BY category)) AS square_root_sales_of_category
FROM products;

This will aggregate the sales by category and display the result for the product category against the product row,

Collapse
 
ashsajal profile image
Xaman

Thanks for the information Reese.

Collapse
 
ashsajal profile image
Xaman

All SQL math functions list : postgresql.org/docs/9.5/functions-...

Collapse
 
ashsajal profile image
Xaman

Don't forget to share your valuable comments!