ChatGPT解决这个技术问题 Extra ChatGPT

Is there a Max function in SQL Server that takes two values like Math.Max in .NET?

I want to write a query like this:

SELECT o.OrderId, MAX(o.NegotiatedPrice, o.SuggestedPrice)
FROM Order o

But this isn't how the MAX function works, right? It is an aggregate function so it expects a single parameter and then returns the MAX of all rows.

Does anyone know how to do it my way?

That's implemented in most other databases as the GREATEST function; SQLite emulates support by allowing multiple columns in the MAX aggregate.
When finding a solution for max(a, b) below keep in mind the question about whether you want the syntax or calculation for "a" and/or "b" to be repeated. I.e. if "b" is derived from a complex calculation involving lots of syntax then you may prefer a solution where "b" appears only once. E.g. the solution "IIF(a>b, a, b)" means repeating "b" – which might be syntactically ugly, however the following solution means "b" (and "a") appear only once: SELECT MAX(VALUE) FROM (SELECT a AS VALUE UNION SELECT b AS VALUE) AS T1
Following up on OMG Ponies' good advice, in the DBMS I've been using, the function is GREATER, rather than GREATEST. So check the help for your DBMS, if you don't find one, try the other, or something similar.

C
Community

If you're using SQL Server 2008 (or above), then this is the better solution:

SELECT o.OrderId,
       (SELECT MAX(Price)
        FROM (VALUES (o.NegotiatedPrice),(o.SuggestedPrice)) AS AllPrices(Price))
FROM Order o

All credit and votes should go to Sven's answer to a related question, "SQL MAX of multiple columns?"
I say it's the "best answer" because:

It doesn't require complicating your code with UNION's, PIVOT's, UNPIVOT's, UDF's, and crazy-long CASE statments. It isn't plagued with the problem of handling nulls, it handles them just fine. It's easy to swap out the "MAX" with "MIN", "AVG", or "SUM". You can use any aggregate function to find the aggregate over many different columns. You're not limited to the names I used (i.e. "AllPrices" and "Price"). You can pick your own names to make it easier to read and understand for the next guy. You can find multiple aggregates using SQL Server 2008's derived_tables like so: SELECT MAX(a), MAX(b) FROM (VALUES (1, 2), (3, 4), (5, 6), (7, 8), (9, 10) ) AS MyTable(a, b)


+1 only answer that doesn't require access to create procedure/functions!
Exactly the type of answer I was looking for. Using functions is slow and this will also work on dates, which is what I need.
+1 Works perfect, especially for more than 2 columns to be compared!
This is less performant than the CASE WHEN solution which only needs to compute a scalar.
While the simpler syntax is may never worth the performance hit when determining the MAX of 2 values, it may be a different matter with more values. Even when obtaining the MAX of 4 values the CASE clauses become long, clumsy and error prone if hand-generated while the VALUES clause remains simple and clear.
s
splattne

Can be done in one line:

-- the following expression calculates ==> max(@val1, @val2)
SELECT 0.5 * ((@val1 + @val2) + ABS(@val1 - @val2)) 

Edit: If you're dealing with very large numbers you'll have to convert the value variables into bigint in order to avoid an integer overflow.


+1 I believe you have provided the most correct way. "SELECT ((@val1+@val2) + ABS(@val1-@val2))/2 as MAX_OF_TWO" Also remember, "SELECT ((@val1+@val2) - ABS(@val1-@val2))/2 as MIN_OF_TWO".
This way will give an overflow error if the sum is greater than can be stored in an int: declare @val1 int declare @val2 int set @val1 = 1500000000 set @val2 = 1500000000 SELECT 0.5 * ((@val1 + @val2) + ABS(@val1 - @val2)) -- => overflow error
This is extremely "dirty" "trick". When programming your code should explicitly express the aim, however in your case it looks like code taken from obfuscation contest.
It may be "dirty", but it could be the only option for databases with simple SQL dialects.
I disagree with marcias. Code doesnt necessarily itself need to explicitly express the aim, as long as comments allow one to work it out. If you are doing any complex mathematical equations in code (or anywhere) its sometimes kind of hard to make it self descriptive. As long as its broken up into simpler, easier to understand parts then that is correct programming.
S
SteveC

You'd need to make a User-Defined Function if you wanted to have syntax similar to your example, but could you do what you want to do, inline, fairly easily with a CASE statement, as the others have said.

The UDF could be something like this:

create function dbo.InlineMax(@val1 int, @val2 int)
returns int
as
begin
  if @val1 > @val2
    return @val1
  return isnull(@val2,@val1)
end

... and you would call it like so ...

SELECT o.OrderId, dbo.InlineMax(o.NegotiatedPrice, o.SuggestedPrice) 
FROM Order o

I would support you solution, the only thing I would add is the support for NULL values. If you simply modify the final line: "return @value2" to read as: "return isnull(@val2,@val1)" then if one of the values is null the function will return the not null value, otherwise it will work as normal
What about other data types e.g. would I need to write a HigherIntegerArgument and a HigherDateTimeArgument and a HigherVarcharArgument and a ...?
this will be incredibly slow, as all things scalar UDFs. Use inline UDFs instead
@xan I have no clue what went through my mind when I actually asked that question. Not too much, obviously. Thanks for the answer anyway.
@Thomas Obligatory meme image (no offence intended to you in any way!) flickr.com/photos/16201371@N00/2375571206
S
Scott Langham

I don't think so. I wanted this the other day. The closest I got was:

SELECT
  o.OrderId,
  CASE WHEN o.NegotiatedPrice > o.SuggestedPrice THEN o.NegotiatedPrice 
     ELSE o.SuggestedPrice
  END
FROM Order o

This is my favorite method. You don't risk an overflow, and it's less cryptic than splattne's solution (which is cool btw), and I don't have the hassle of creating a UDF. case is very handy in many situations.
SELECT o.OrderId, CASE WHEN o.NegotiatedPrice > o.SuggestedPrice OR o.SuggestedPrice IS NULL THEN o.NegotiatedPrice ELSE o.SuggestedPrice END FROM Order o
When instead of "o.NegotiatedPrice" you have rather a term like "(datediff(day, convert(datetime, adr_known_since, 120), getdate())-5)*0.3" you have to repeat this code. Any future changes to the term have to be done twice. A min(x, y, ...) type function would be much nicer
This answer seems to be outdated: GREATEST exists in TSQL: docs.microsoft.com/en-us/sql/t-sql/functions/…
X
Xin

Why not try IIF function (requires SQL Server 2012 and later)

IIF(a>b, a, b)

That's it.

(Hint: be careful about either would be null, since the result of a>b will be false whenever either is null. So b will be the result in this case)


If one of the values is NULL, the result will always be the second one.
IIF() is syntactic sugar for the CASE statement. If either value of the CASE conditional is NULL, the result will be the second one (ELSE).
@xxyzzy that is because NULL > 1234 statement is false
so IIF(a>b, a, COALESCE(b,a)) to give the value when only one exists
M
Martin Smith
DECLARE @MAX INT
@MAX = (SELECT MAX(VALUE) 
               FROM (SELECT 1 AS VALUE UNION 
                     SELECT 2 AS VALUE) AS T1)

I give this solution a +1 because it conforms to DRY (don't repeat yourself) without the need to write a UDF. It's also great if both the values you need to check are the results of other sql, eg in my case I want to find the greater of 2 select count(*) statements.
I hate that I have to resort to this solution, but it's for sure the best way to do it in SQL Server until they add native support for GREATEST or in-line MAX. Thanks for posting it - +1 to you!
L
LukStorms

In SQL Server 2012 or higher, you can use a combination of IIF and ISNULL (or COALESCE) to get the maximum of 2 values.
Even when 1 of them is NULL.

IIF(col1 >= col2, col1, ISNULL(col2, col1)) 

Or if you want it to return 0 when both are NULL

IIF(col1 >= col2, col1, COALESCE(col2, col1, 0)) 

Example snippet:

-- use table variable for testing purposes
declare @Order table 
(
  OrderId int primary key identity(1,1),
  NegotiatedPrice decimal(10,2),
  SuggestedPrice decimal(10,2)
);

-- Sample data
insert into @Order (NegotiatedPrice, SuggestedPrice) values
(0, 1),
(2, 1),
(3, null),
(null, 4);

-- Query
SELECT 
     o.OrderId, o.NegotiatedPrice, o.SuggestedPrice, 
     IIF(o.NegotiatedPrice >= o.SuggestedPrice, o.NegotiatedPrice, ISNULL(o.SuggestedPrice, o.NegotiatedPrice)) AS MaxPrice
FROM @Order o

Result:

OrderId NegotiatedPrice SuggestedPrice  MaxPrice
1       0,00            1,00            1,00
2       2,00            1,00            2,00
3       3,00            NULL            3,00
4       NULL            4,00            4,00

But if one needs the maximum of multiple columns? Then I suggest a CROSS APPLY on an aggregation of the VALUES.

Example:

SELECT t.*
, ca.[Maximum]
, ca.[Minimum], ca.[Total], ca.[Average]
FROM SomeTable t
CROSS APPLY (
   SELECT 
    MAX(v.col) AS [Maximum], 
    MIN(v.col) AS [Minimum], 
    SUM(v.col) AS [Total], 
    AVG(v.col) AS [Average]
   FROM (VALUES (t.Col1), (t.Col2), (t.Col3), (t.Col4)) v(col)
) ca

This has the extra benefit that this can calculate other things at the same time.


佚名

The other answers are good, but if you have to worry about having NULL values, you may want this variant:

SELECT o.OrderId, 
   CASE WHEN ISNULL(o.NegotiatedPrice, o.SuggestedPrice) > ISNULL(o.SuggestedPrice, o.NegotiatedPrice)
        THEN ISNULL(o.NegotiatedPrice, o.SuggestedPrice)
        ELSE ISNULL(o.SuggestedPrice, o.NegotiatedPrice)
   END
FROM Order o

The only required ISNULL is after the ELSE. The initial ">" comparison will return false and go to the ELSE if either of the values is null already.
M
Martin Smith

Sub Queries can access the columns from the Outer query so you can use this approach to use aggregates such as MAX across columns. (Probably more useful when there is a greater number of columns involved though)

;WITH [Order] AS
(
SELECT 1 AS OrderId, 100 AS NegotiatedPrice, 110 AS SuggestedPrice UNION ALL
SELECT 2 AS OrderId, 1000 AS NegotiatedPrice, 50 AS SuggestedPrice
)
SELECT
       o.OrderId, 
       (SELECT MAX(price)FROM 
           (SELECT o.NegotiatedPrice AS price 
            UNION ALL SELECT o.SuggestedPrice) d) 
        AS MaxPrice 
FROM  [Order]  o

Nice! It scales up very well.
+1 to show Love for those still on 2005. I don't know how I overlooked this answer. Under the covers, I imagine it performs just as well as what I posted 2-years later. In retrospect, I should have realized this and updated your answer to include the newer 2008 syntax at the time. Sorry, wish I could share my points with you now.
@MikeTeeVee - Thanks! Yes under the covers the plan will be the same. But the VALUES syntax is nicer.
good answer because it works across all versions including the new Azure DW/synapse, which doesn't support VALUES()
C
Chris Rogers

Try this. It can handle more than 2 values

SELECT Max(v) FROM (VALUES (1), (2), (3)) AS value(v)

Super! I wrote a solution using GREATEST that runs on our AZURE SQL Server, but this solution also runs on my desktop SQL Server Express
This is the best solution. Especially if your values are derived from complex functions.
T
Tom Arleth
SELECT o.OrderId,   
--MAX(o.NegotiatedPrice, o.SuggestedPrice)  
(SELECT MAX(v) FROM (VALUES (o.NegotiatedPrice), (o.SuggestedPrice)) AS value(v)) as ChoosenPrice  
FROM Order o

For explanation please consult this article: red-gate.com/simple-talk/sql/sql-training/…
Please don't include needed information to your code just by a link. Imagine that this link will expire one day and your answer will be useless then. So please go ahead and add the essentiell information directly in your answer. But you can still provide that link as a ressource for others to look further information up.
C
Community

I would go with the solution provided by kcrumley Just modify it slightly to handle NULLs

create function dbo.HigherArgumentOrNull(@val1 int, @val2 int)
returns int
as
begin
  if @val1 >= @val2
    return @val1
  if @val1 < @val2
    return @val2

 return NULL
end

EDIT Modified after comment from Mark. As he correctly pointed out in 3 valued logic x > NULL or x < NULL should always return NULL. In other words unknown result.


Nulls are important. And it's important to handle them consistently. The only proper answer to Is NULL > x is NULL.
You are right, i will modify my answer to reflect that, thanks for pointing that out
If we pass an int and a NULL then I think it's more common to want the non-null value returned, so the function is acting as a combination of Max(x,y) and ISNULL(x,y). Hence I personally would change the last line to be: return ISNULL(@val1, @val2) - which admittedly is probably what you had to start with :)
@the-locster, see comment by Mark
this will be incredibly slow, as all things scalar UDFs. Use inline UDFs instead
S
SetFreeByTruth

SQL Server 2012 introduced IIF:

SELECT 
    o.OrderId, 
    IIF( ISNULL( o.NegotiatedPrice, 0 ) > ISNULL( o.SuggestedPrice, 0 ),
         o.NegotiatedPrice, 
         o.SuggestedPrice 
    )
FROM 
    Order o

Handling NULLs is recommended when using IIF, because a NULL on either side of your boolean_expression will cause IIF to return the false_value (as opposed to NULL).


Your solution will not handle NULL well when the other value is negative, this will return null
M
Mark Brackett

I probably wouldn't do it this way, as it's less efficient than the already mentioned CASE constructs - unless, perhaps, you had covering indexes for both queries. Either way, it's a useful technique for similar problems:

SELECT OrderId, MAX(Price) as Price FROM (
   SELECT o.OrderId, o.NegotiatedPrice as Price FROM Order o
   UNION ALL
   SELECT o.OrderId, o.SuggestedPrice as Price FROM Order o
) as A
GROUP BY OrderId

C
Community

Oops, I just posted a dupe of this question...

The answer is, there is no built in function like Oracle's Greatest, but you can achieve a similar result for 2 columns with a UDF, note, the use of sql_variant is quite important here.

create table #t (a int, b int) 

insert #t
select 1,2 union all 
select 3,4 union all
select 5,2

-- option 1 - A case statement
select case when a > b then a else b end
from #t

-- option 2 - A union statement 
select a from #t where a >= b 
union all 
select b from #t where b > a 

-- option 3 - A udf
create function dbo.GREATEST
( 
    @a as sql_variant,
    @b as sql_variant
)
returns sql_variant
begin   
    declare @max sql_variant 
    if @a is null or @b is null return null
    if @b > @a return @b  
    return @a 
end


select dbo.GREATEST(a,b)
from #t

kristof

Posted this answer:

create table #t (id int IDENTITY(1,1), a int, b int)
insert #t
select 1,2 union all
select 3,4 union all
select 5,2

select id, max(val)
from #t
    unpivot (val for col in (a, b)) as unpvt
group by id

Note: the GREATEST function implementation will match the oracle behavior for 2 params, if any param is null it will return null
You should be careful when using sql_variant. Your function will give an unexpected result in the following situation: SELECT dbo.greatest(CAST(0.5 AS FLOAT), 100)
@Neil is right (I learned it the hard way), how would you improve this function to prevent this kind of problems?
U
Uri Abramson

Its as simple as this:

CREATE FUNCTION InlineMax
(
    @p1 sql_variant,
    @p2 sql_variant
)  RETURNS sql_variant
AS
BEGIN
    RETURN CASE 
        WHEN @p1 IS NULL AND @p2 IS NOT NULL THEN @p2 
        WHEN @p2 IS NULL AND @p1 IS NOT NULL THEN @p1
        WHEN @p1 > @p2 THEN @p1
        ELSE @p2 END
END;

See @Neil comment to a previous answer SELECT dbo.InlineMax(CAST(0.5 AS FLOAT), 100) is wrong.
L
Lukasz Szozda

YES, THERE IS.

T-SQL (SQL Server 2022 (16.x)) now supports GREATEST/LEAST functions:

MAX/MIN as NON-aggregate function This is now live for Azure SQL Database and SQL Managed Instance. It will roll into the next version of SQL Server.

Logical Functions - GREATEST (Transact-SQL)

This function returns the maximum value from a list of one or more expressions. GREATEST ( expression1 [ ,...expressionN ] )

So in this case:

SELECT o.OrderId, GREATEST(o.NegotiatedPrice, o.SuggestedPrice)
FROM Order o

You won't find these functions on SQL Server 2019 (150) or earlier.
I think GREATEST only currently is available on SQL Server Azure
P
Per Hornshøj-Schierbeck

You can do something like this:

select case when o.NegotiatedPrice > o.SuggestedPrice 
then o.NegotiatedPrice
else o.SuggestedPrice
end

W
Wayne
SELECT o.OrderID
CASE WHEN o.NegotiatedPrice > o.SuggestedPrice THEN
 o.NegotiatedPrice
ELSE
 o.SuggestedPrice
END AS Price

else isnull( oSuggestedPrice, o,NegotiatedPrice )
d
deepee1

For the answer above regarding large numbers, you could do the multiplication before the addition/subtraction. It's a bit bulkier but requires no cast. (I can't speak for speed but I assume it's still pretty quick)

SELECT 0.5 * ((@val1 + @val2) + ABS(@val1 - @val2))

Changes to

SELECT @val1*0.5+@val2*0.5 + ABS(@val1*0.5 - @val2*0.5)

at least an alternative if you want to avoid casting.


s
scradam

Here's a case example that should handle nulls and will work with older versions of MSSQL. This is based on the inline function in one one of the popular examples:

case
  when a >= b then a
  else isnull(b,a)
end

a
ashraf mohammed
 -- Simple way without "functions" or "IF" or "CASE"
 -- Query to select maximum value
 SELECT o.OrderId
  ,(SELECT MAX(v)
   FROM (VALUES (o.NegotiatedPrice), (o.SuggestedPrice)) AS value(v)) AS MaxValue
  FROM Order o;

While interesting use of VALUES inline like that, I'm not sure this is simpler than CASE or IFF. I'd be interested to see how the performance of this solution stacks up against the other options though
@ChrisSchaller the interesting part with this usage of VALUESis that it provides an easy way to check more than one column for the MAX value. As for the performance, I do not know :)
@mortb this answer is a blatant rip off the earlier one from stackoverflow.com/a/52296106/1690217 I'm starting to see the value in this, but not sure I would call it "simple" We're effectively UNPIVOTing the values so that we can use an aggregate over the inner set. Its elegant, low code, but complex to understand.
s
sth
CREATE FUNCTION [dbo].[fnMax] (@p1 INT, @p2 INT)
RETURNS INT
AS BEGIN

    DECLARE @Result INT

    SET @p2 = COALESCE(@p2, @p1)

    SELECT
        @Result = (
                   SELECT
                    CASE WHEN @p1 > @p2 THEN @p1
                         ELSE @p2
                    END
                  )

    RETURN @Result

END

m
mohghaderi

Here is @Scott Langham's answer with simple NULL handling:

SELECT
      o.OrderId,
      CASE WHEN (o.NegotiatedPrice > o.SuggestedPrice OR o.SuggestedPrice IS NULL) 
         THEN o.NegotiatedPrice 
         ELSE o.SuggestedPrice
      END As MaxPrice
FROM Order o

j
jahu

Here is an IIF version with NULL handling (based on of Xin's answer):

IIF(a IS NULL OR b IS NULL, ISNULL(a,b), IIF(a > b, a, b))

The logic is as follows, if either of the values is NULL, return the one that isn't NULL (if both are NULL, a NULL is returned). Otherwise return the greater one.

Same can be done for MIN.

IIF(a IS NULL OR b IS NULL, ISNULL(a,b), IIF(a < b, a, b))

e
error
select OrderId, (
    select max([Price]) from (
        select NegotiatedPrice [Price]
        union all
        select SuggestedPrice
    ) p
) from [Order]

J
Jason Plank

In its simplest form...

CREATE FUNCTION fnGreatestInt (@Int1 int, @Int2 int )
RETURNS int
AS
BEGIN

    IF @Int1 >= ISNULL(@Int2,@Int1)
        RETURN @Int1
    ELSE
        RETURN @Int2

    RETURN NULL --Never Hit

END

S
Steve Ford

For SQL Server 2012:

SELECT 
    o.OrderId, 
    IIF( o.NegotiatedPrice >= o.SuggestedPrice,
         o.NegotiatedPrice, 
         ISNULL(o.SuggestedPrice, o.NegiatedPrice) 
    )
FROM 
    Order o

C
Chris Porter

Expanding on Xin's answer and assuming the comparison value type is INT, this approach works too:

SELECT IIF(ISNULL(@A, -2147483648) > ISNULL(@B, -2147483648), @A, @B)

This is a full test with example values:

DECLARE @A AS INT
DECLARE @B AS INT

SELECT  @A = 2, @B = 1
SELECT  IIF(ISNULL(@A, -2147483648) > ISNULL(@B, -2147483648), @A, @B)
-- 2

SELECT  @A = 2, @B = 3
SELECT  IIF(ISNULL(@A, -2147483648) > ISNULL(@B, -2147483648), @A, @B)
-- 3

SELECT  @A = 2, @B = NULL
SELECT  IIF(ISNULL(@A, -2147483648) > ISNULL(@B, -2147483648), @A, @B)
-- 2    

SELECT  @A = NULL, @B = 1
SELECT  IIF(ISNULL(@A, -2147483648) > ISNULL(@B, -2147483648), @A, @B)
-- 1

D
Desert Eagle

In MemSQL do the following:

-- DROP FUNCTION IF EXISTS InlineMax;
DELIMITER //
CREATE FUNCTION InlineMax(val1 INT, val2 INT) RETURNS INT AS
DECLARE
  val3 INT = 0;
BEGIN
 IF val1 > val2 THEN
   RETURN val1;
 ELSE
   RETURN val2;
 END IF; 
END //
DELIMITER ;

SELECT InlineMax(1,2) as test;