{"id":1543,"date":"2021-04-29T11:23:16","date_gmt":"2021-04-29T09:23:16","guid":{"rendered":"https:\/\/sqlkover.com\/?p=1543"},"modified":"2021-04-29T13:02:44","modified_gmt":"2021-04-29T11:02:44","slug":"the-greatest-new-t-sql-function","status":"publish","type":"post","link":"https:\/\/sqlkover.com\/the-greatest-new-t-sql-function\/","title":{"rendered":"The GREATEST new T-SQL Function"},"content":{"rendered":"<body>\n<p>If the title isn\u2019t obvious enough, this blog post is about the new function <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/functions\/logical-functions-greatest-transact-sql?view=sql-server-ver15\" target=\"_blank\" rel=\"noreferrer noopener\">GREATEST<\/a>, and it\u2019s sibling <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/functions\/logical-functions-least-transact-sql?view=sql-server-ver15\" target=\"_blank\" rel=\"noreferrer noopener\">LEAST<\/a>. But wait, how come there\u2019s a new T-SQL function? There isn\u2019t a new release of SQL Server! That\u2019s right, those two new functions are released in Azure SQL DB, Azure SQL Managed Instance and Azure Synapse Analytics. In other words, cloud first, on-prem later. I learned about those functions when reading the blog post <a href=\"https:\/\/www.brentozar.com\/archive\/2021\/04\/your-top-10-ideas-for-sql-server\/?utm_source=feedburner&amp;utm_medium=feed&amp;utm_campaign=Feed%3A+BrentOzar-SqlServerDba+%28Brent+Ozar+Unlimited%29\" target=\"_blank\" rel=\"noreferrer noopener\">Your Top 10 Ideas for SQL Server<\/a> by Brent Ozar, where he subtle tricks people in commenting about which features they want in SQL Server, and then he scolds them for not having submit a feature request \ud83d\ude09 Anyway, someone in the comments mentioned the discovery of the 2 new functions.<\/p>\n\n\n\n<p>So what do they do exactly? You can look at GREATEST as a MAX function, but for columns instead of rows. Let\u2019s demonstrate with an example. We got the following simple table:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT 1 AS Col1,2 AS Col2,3 AS Col3\nINTO #Test\nUNION ALL\nSELECT 6,4,5\nUNION ALL\nSELECT 10,-5,9;\n\nSELECT *\nFROM #Test;<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image.png\"><img decoding=\"async\" width=\"181\" height=\"118\" src=\"https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image.png\" alt=\"\" class=\"wp-image-1544\" loading=\"lazy\"><\/a><\/figure>\n\n\n\n<p>To get the highest value out of the 3 columns for each row, we can now use the following statement:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"classic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT GREATEST(Col1,Col2,Col3)\nFROM #Test;<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-1.png\"><img decoding=\"async\" width=\"415\" height=\"188\" src=\"https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-1.png\" alt=\"\" class=\"wp-image-1545\" loading=\"lazy\" srcset=\"https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-1.png 415w, https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-1-300x136.png 300w\" sizes=\"auto, (max-width: 415px) 100vw, 415px\" \/><\/a><\/figure>\n\n\n\n<p>As you can see from the screenshot, Intellisense doesn\u2019t recognize this great function (pun intended) yet. I really like this addition, as in ye olde times we had to write a lot of nested CASE\/IIF functions to get the job done (or you could pivot\/unpivot the data and use MAX if that\u2019s your thing). And it only got more harder if more columns were involved. Also, it was one of the things that Oracle had, but SQL Server didn\u2019t. So it gets easier to migrate PL-SQL to T-SQL \ud83d\ude42<\/p>\n\n\n\n<p>LEAST does the opposite of course:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-2.png\"><img decoding=\"async\" width=\"383\" height=\"181\" src=\"https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-2.png\" alt=\"\" class=\"wp-image-1546\" loading=\"lazy\" srcset=\"https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-2.png 383w, https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-2-300x142.png 300w\" sizes=\"auto, (max-width: 383px) 100vw, 383px\" \/><\/a><\/figure>\n\n\n\n<p>The functions work with any comparable data type, such as dates:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-3.png\"><img decoding=\"async\" width=\"624\" height=\"139\" src=\"https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-3.png\" alt=\"\" class=\"wp-image-1547\" loading=\"lazy\" srcset=\"https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-3.png 624w, https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-3-300x67.png 300w\" sizes=\"auto, (max-width: 624px) 100vw, 624px\" \/><\/a><\/figure>\n\n\n\n<p>And luckily, they ignore NULL values:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-4.png\"><img decoding=\"async\" width=\"378\" height=\"221\" src=\"https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-4.png\" alt=\"\" class=\"wp-image-1548\" loading=\"lazy\" srcset=\"https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-4.png 378w, https:\/\/sqlkover.com\/wp-content\/uploads\/2021\/04\/image-4-300x175.png 300w\" sizes=\"auto, (max-width: 378px) 100vw, 378px\" \/><\/a><\/figure>\n\n\n\n<p>UPDATE: I\u2019ve used \u201ccolumn\u201d throughout this blog post, but \u201cexpressions\u201d is more accurate. This can be a column, a subquery, a variable, a calculation and so on.<\/p>\n<\/body>","protected":false},"excerpt":{"rendered":"<p>If the title isn\u2019t obvious enough, this blog post is about the new function GREATEST, and it\u2019s sibling LEAST. But wait, how come there\u2019s a new T-SQL function? There isn\u2019t a new release of SQL Server! That\u2019s right, those two new functions are released in Azure SQL DB, Azure SQL Managed Instance and Azure Synapse [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[55],"tags":[191,19,56],"class_list":["post-1543","post","type-post","status-publish","format-standard","hentry","category-tsql","tag-sql","tag-sql-server","tag-tsql"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/sqlkover.com\/wp-json\/wp\/v2\/posts\/1543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sqlkover.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlkover.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlkover.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlkover.com\/wp-json\/wp\/v2\/comments?post=1543"}],"version-history":[{"count":1,"href":"https:\/\/sqlkover.com\/wp-json\/wp\/v2\/posts\/1543\/revisions"}],"predecessor-version":[{"id":1551,"href":"https:\/\/sqlkover.com\/wp-json\/wp\/v2\/posts\/1543\/revisions\/1551"}],"wp:attachment":[{"href":"https:\/\/sqlkover.com\/wp-json\/wp\/v2\/media?parent=1543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlkover.com\/wp-json\/wp\/v2\/categories?post=1543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlkover.com\/wp-json\/wp\/v2\/tags?post=1543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}