r/SQLServer • u/columbaspexit • Apr 17 '21
Next version of SQL Server will have GREATEST/LEAST functions
https://feedback.azure.com/forums/908035-sql-server/suggestions/32902729-max-min-as-non-aggregate-function12
u/dmarkle Apr 17 '21
I feel like a whole book could be written on the culture of resistance to new, simple features in the SQL language in general, and SQL Server more specifically.
8
u/da_chicken Apr 17 '21
For a very long time MS was only adding features that fit a very narrow range. Part of that was the massive redesign for 2005 and 2008, but mostly it's MS's poor organization and stack-ranking.
Take indexed views, for example. If you come up with real world examples of when you'd actually want to have what they do, you'll find that the limitations on them make them useless in about 5 out of 6 of them. They're virtually worthless, but they fill a checkbox on a white paper or a sales brochure. They do the thing, just not really.
I understand the motivation not to have string aggregation and disaggregation functions, but... they're so useful. 2017 is really, really late to be adding that.
7
u/Prequalified Apr 17 '21
Indexed views are worse than useless because they double the amount of indexes you need to maintain, not to mention the inability to use disk saving features like clustered column stores.
5
Apr 17 '21
[removed] — view removed comment
-4
u/da_chicken Apr 17 '21
Actually I disagree on that one. A table is an unordered set of rows, so string_split() correctly doesn't care about order. If you need to store data and it's order, you need to store two data elements and you should create a table.
I'm willing to accept violation of 1NF in situations where the data are very simple, predictable, and (critically) rarely used. However, you're still breaking 1NF. I really don't have any sympathy for you if you run into query trouble when you do that.
2
u/MetiLee Apr 17 '21
The whole purpose of the language is to have a language universal, to synch providers to add an extra function is not something feasible, this is why it takes a long time to add something to the syntax
4
u/elus Apr 17 '21
The ANSI SQL standard can provide a roadmap for vendors to follow but some choose to ignore it for years until their customer base is sufficiently annoyed.
3
u/MikeScalise Apr 17 '21 edited Apr 17 '21
This may be common knowledge, but here's what I do when I need to get the greatest and/or least value from a list of values/fields. It may not be as efficient as the GREATEST/LEAST functions in the next version of SQL Server will be, but it's at least a workable approach for 2019 and prior.
SELECT (SELECT MAX(val) FROM (VALUES (10), (30), (20), (5)) val_tbl(val)) AS Greatest,
(SELECT MIN(val) FROM (VALUES (10), (30), (20), (5)) val_tbl(val)) AS Least;or from a table of values:
SELECT t.val1,
t.val2,
t.val3,
(SELECT MAX(val) FROM (VALUES (t.val1), (t.val2), (t.val3)) val_tbl(val)) AS Greatest,
(SELECT MIN(val) FROM (VALUES (t.val1), (t.val2), (t.val3)) val_tbl(val)) AS Least
FROM (VALUES (151,275,179),
(64,268,240),
(216,29,156),
(252,271,200)) t(val1, val2, val3);or even from a table like "Posts" from the StackOverflow database:
SELECT p.Id,
p.AnswerCount,
p.CommentCount,
(SELECT MAX(val) FROM (VALUES (p.AnswerCount), (p.CommentCount)) val_tbl(val)) AS Greatest,
(SELECT MIN(val) FROM (VALUES (p.AnswerCount), (p.CommentCount)) val_tbl(val)) AS Least
FROM Posts p;EDIT: Formatting.
12
u/columbaspexit Apr 17 '21 edited Apr 17 '21
Original request:
Please add the GREATEST/LEAST functions, just like in PostgreSQL/Oracle (with the same name for syntax-compatibility). SQL-Server lacks functions for MAX/MIN as NON-aggregate function. e.g. When I need the MAXIMUM of two values, or the minimum of two values (in columns, not rows)...
& latest response from the SQL Server product manager:
SUPER looking forward to not having to use clunky udf's or long case statements anymore.