Linked Questions
25 questions linked to/from Max function in SQL Server that takes two values like Math.Max in .NET
7
votes
1
answer
50k
views
Return zero if value less than zero [duplicate]
Is it possible to return zero if a value is less than zero without using a case statement?
e.g. Max(a, 0) <-- 'Max' doesn't exist.
I would like my code to be as succinct as possible.
10
votes
4
answers
17k
views
T-SQL equivalent of Excel "MAX" function to return larger of two numbers [duplicate]
Possible Duplicate:
Is there a Max function in SQL Server that takes two values like Math.Max in .NET?
In Excel, there's a function called "MAX" that accepts numbers and returns the largest one in ...
3
votes
3
answers
175
views
Returning largest value across columns in SQL Server 2008 [duplicate]
In SQL Server 2008, I need to query across columns in T-SQL and return the largest number I find (example below using dates, but same idea applies to numbers). "NULL" indicates a null value, not the ...
1
vote
0
answers
254
views
Insert a value restricted to upper and lower bounds [duplicate]
I need to do an insert where one column is to be a calculated numerical value based on a value from another table, yet restricted between upper and lower bounds. Let's call these @lower_bounds and @...
546
votes
24
answers
927k
views
SQL MAX of multiple columns?
How do you return 1 value per row of the max of several columns:
TableName
[Number, Date1, Date2, Date3, Cost]
I need to return something like this:
[Number, Most_Recent_Date, Cost]
Query?
121
votes
21
answers
395k
views
What's the best way to select the minimum value from several columns?
Given the following table in SQL Server 2005:
ID Col1 Col2 Col3
-- ---- ---- ----
1 3 34 76
2 32 976 24
3 7 235 3
4 245 1 792
What is ...
278
votes
2
answers
145k
views
How to get min/max of two integers in Postgres/SQL?
How do I find the maximum (or minimum) of two integers in Postgres/SQL? One of the integers is not a column value.
I will give an example scenario:
I would like to subtract an integer from a column (...
52
votes
8
answers
117k
views
How to calculate the maximum of two numbers in Oracle SQL select?
This should be simple and shows my SQL ignorance:
SQL> select max(1,2) from dual;
select max(1,2) from dual
*
ERROR at line 1:
ORA-00909: invalid number of arguments
I know max is normally ...
17
votes
4
answers
7k
views
In SQL, are there non-aggregate min / max operators
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 ...
6
votes
6
answers
15k
views
SQL - Select the largest value within a row
I seem to be stuck on this and can't find a solution having had a look around.
I have an SQL table who's first row looks something like this:
Name Val1 Val2 Val3
John 1000 2000 3000
What ...
3
votes
4
answers
9k
views
Cap field value in SELECT
I am trying to cap particular fields so that they don't exceed a particular value.
For example, something like this would suffice, where I can specify that a field should not exceed 8:
SELECT (
...
10
votes
4
answers
7k
views
Scalar Max in Sql Server
How to implement the scalar MAX in Sql server (like Math.Max). (In essense I want to implement something like Max(expression, 0), to make negative values replaced by 0.)
I've seen in other threads ...
2
votes
3
answers
12k
views
SQL Server equivalent to Oracle LEAST?
I've been looking for a good equivalent to the Oracle LEAST function.
I'm hoping to implement a user defined function that does about 10 fairly complex calculations, and takes the minimum value from ...
3
votes
4
answers
4k
views
Have an upper limit for SUM() function sql
I'm doing a small calculation in the select statement of an sql query along the lines of this:
SELECT
(select sum(weighting)
from table1
where id = tablemain.id) *
(select sum(...
4
votes
0
answers
21k
views
Greatest of multiple values in SQL Server [duplicate]
Possible Duplicate:
SQL Server equivalent to Oracle LEAST?
Function in SQL Server 2008 similar to GREATEST in mysql?
i am trying to find a SQL Server equivalent of oracle's "GREATEST" function.
...