Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQL, are there non-aggregate min / max operators

Tags:

sql

sql-server

Is there something like

select max(val,0)
from table

I'm NOT looking to find the maximum value of the entire table

There has to be an easier way than this right?

select case when val > 0 then val else 0 end
from table

EDIT: I'm using Microsoft SQL Server

like image 870
Colin Avatar asked Sep 30 '11 16:09

Colin


People also ask

What are non aggregate functions in SQL?

Aggregate functions operate on many records and produce a summary, works with GROUP BY whereas non-aggregate functions operate on each record independently. There are so many built-in functions in SQL to do various calculations on data.

Can we use MAX function without GROUP BY?

Using MIN() and MAX() in the Same Query You can use both the MIN and MAX functions in one SELECT . If you use only these functions without any columns, you don't need a GROUP BY clause.

Can we use non aggregate function in HAVING clause?

3. The having clause can contain aggregate functions. It cannot contain aggregate functions.


1 Answers

Functions GREATEST and LEAST are not SQL standard but are in many RDBMSs (e.g., Postgresql). So

SELECT GREATEST(val, 0) FROM mytable;
like image 171
Andrew Lazarus Avatar answered Sep 30 '22 16:09

Andrew Lazarus