r/SQLServer • • Nov 24 '20

Question What the heck?! When did SQL Server start supporting GREATEST and LEAST?

Even my SQL Prompt doesn't recognize it, but by golly it works!

(I'm in SQL Managed Instance)

SELECT GREATEST(1, 2, 4) --> Returns 4

60 Upvotes

36 comments sorted by

7

u/Scrapper_John Nov 24 '20

Is it different from max and min?

21

u/AsleepOnTheTrain Nov 24 '20

Yes, max and min give you the greatest/least value in a column across rows.

Greatest and least give you the greatest/least values in a row across columns.

ClassBoysGirls
Freshman12
Sophomore34
Junior56
Senior78

SELECT MAX(Boys), MAX(Girls) FROM Students --> 7, 8 - because those are the highest numbers in each column SELECT Class, GREATEST(Boys, Girls) FROM Students WHERE Class = 'Freshman' --> Freshman, 2 - because 2 is the highest number across the Boys and Girls columns for that row.

7

u/Scrapper_John Nov 24 '20

Ok, now I understand. Thank you.

2

u/logandunning Feb 21 '21

Thank you so much this truly help me understand. What a powerful function. theoretically you’d have to write a huge case statement if you had let’s say 10 different columns you’re comparing the size. I wonder under the hood what’s more efficient, big case statements or least and greatest. It would be amazing to see some benchmarks, I normally blindly except that any DBMS function implementation would be better than what I would write. So I assume that these would be more efficient, least and greatest. So happy about this function. Thank you so much for explaining it so perfectly

5

u/ellibob17 Nov 24 '20

max and min are aggregate functions that require grouping/partitions

GREATEST/LEAST are single row expressions for comparisons between several columns/expressions

1

u/Scrapper_John Nov 24 '20

So no group by? Is the optimizer just doing a group by on all select list elements behind the scene?

4

u/ellibob17 Nov 24 '20

No, the GREATEST/LEAST functions do nothing across rows.

They are essentially "Case when A>B then A. etc..." (though I don't know if they are implemented this way)

13

u/tsqladdict Nov 24 '20

Holy crap. This is the greatest news I've heard in a while!

(Also, just tested in Azure SQL DB. It works.)

6

u/Shinob1 Nov 25 '20

Pun intended?

13

u/fackert18 Nov 25 '20

Not in the least.

9

u/blackdonkey Nov 24 '20

Hmm I can't find any MSFT documentation on it.

0

u/[deleted] Nov 24 '20

[deleted]

2

u/blackdonkey Nov 24 '20

Can you check what version you are on? And what IDE you are using?

9

u/TedDallas Nov 25 '20

Yep. Tried it. This works. Then I tried it on an Azure SQL Data Warehouse/Synapse instance and it does not work there. But this is not surprising. Maybe it will be available there by 2042.

5

u/a_small_goat Nov 24 '20

Nice - definitely a new addition. Other DBMS supported it but in SQL it usually took some sort of Rube Goldberg query involving CROSS APPLY or CASE or whatever.

3

u/ScotJoplin Nov 25 '20

Probably just a new feature they’re going to release in future. It can be turned on or off on Azure on a whim of Microsoft’s. Might stop working soon, might become an official feature.

2

u/Scrapper_John Nov 24 '20

What version of SQL server are you running?

0

u/[deleted] Nov 24 '20

[deleted]

2

u/Scrapper_John Nov 24 '20

Ok, I tried it on SSMS 2014 12.0.2008.8 and it didn’t work.

1

u/Scrapper_John Nov 24 '20

Out of curiosity what version of SQL Server are you running? Select @@VERSION

0

u/[deleted] Nov 24 '20

[deleted]

2

u/Scrapper_John Nov 24 '20

No, at work I’m on SQL server 2017 with a 2014 install of SSMS. I really need to ask for an upgrade. But on my personal laptop I have 2019 SQL Server and SSMS 16.7 and it didn’t work there either. So I’m guessing those functions only work with the Azure engine.

1

u/alinroc 4 Nov 25 '20

SSMS 16.7

Ugh. You're missing out on a lot of important stuff that's in the latest version, especially WRT Query Store and execution plan details.

0

u/[deleted] Nov 24 '20

[deleted]

3

u/Scrapper_John Nov 24 '20

You have no idea. As a report developer I don’t even get write access to any of the databases. So I have to create what would normally be a view or stored procedure in temp..db. Then they get wrapped in Crystal reports or Cognos, which Crystal reports has a 56k character limit. So instead of creating a table, sometimes I get to make 5 or 6 global temp tables for hard coded values, and then update the reports each year for the new specs.

1

u/chadbaldwin Nov 28 '20

Holy crap. This is why developers should not always be blamed for writing crappy code.

If I took over your project years later and saw that mess, I'd probably call you an idiot, not realizing you were simply working with what you were given.

That's pretty upsetting to hear that they are basically forcing you to build something that's so unmanageable.

0

u/[deleted] Nov 25 '20

[deleted]

1

u/fackert18 Nov 25 '20

Without some controls it can get out of hand over time, but having a play area as a report writer is key. We don’t have write access to most databases, but I wouldn’t be able to effectively do my job without our PA database on the prod sever. Though it sometimes gets referred to as the “Production Area” given some of the critical processes that use it.

1

u/cosmokenney Nov 24 '20

hmm... that's a new one on me!

1

u/Leroy_UK Nov 24 '20

April Fools?

1

u/ZeppelinJ0 Nov 25 '20

Holy fuck

1

u/mauricesql Dec 01 '20

It is quite easy to do that with a cross apply. Greatest / least are not things that need to be used often.

Select greatest.r From SampleTableWithColsABC as s Cross apply (select r=max(I) from (values (s.a), (s.b), (s.c)) as tochk(r)) as greatest

Cross apply allows passing values from cols A B and C to the values expression which make pseudo table given in input to the max() aggregate function. Check the access plan. This is low overhead and quite fast. There is even no temp table created. This is a syntaxic trick to turn a fix length list into a table which can then be processed as normal by Sql.

1

u/mauricesql Dec 01 '20

It is quite easy to do that with a cross apply. Greatest / least are not things that need to be used often.

Select greatest.r From SampleTableWithColsABC as s Cross apply (select r=max(I) from (values (s.a), (s.b), (s.c)) as tochk(r)) as greatest

Cross apply allows passing values from cols A B and C to the values expression which make pseudo table given in input to the max() aggregate function. Check the access plan. This is low overhead and quite fast. There is even no temp table created. This is a syntaxic trick to turn a fix length list into a table which can then be processed as normal by Sql.

0

u/[deleted] Nov 25 '20

[deleted]

1

u/Prequalified Nov 25 '20

Check your compatibility version. It’s possible you have a lower compatibility version even with a newer SQL server version. I usually have my DBs set to one of the most recent versions (I think 150).