r/SQLServer • u/AsleepOnTheTrain • 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
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
9
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
Nov 24 '20
[deleted]
2
1
u/Scrapper_John Nov 24 '20
Out of curiosity what version of SQL Server are you running? Select @@VERSION
0
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
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
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
1
1
1
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
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).
7
u/Scrapper_John Nov 24 '20
Is it different from max and min?