Introducing the GREATEST and LEAST T-SQL functions in Azure Synapse Analytics

This article is contributed. See the original author and article here.


We are excited to announce that the GREATEST and LEAST T-SQL functions are now generally available in Azure Synapse Analytics (serverless SQL pools only).


This post describes the functionality and common use cases of GREATEST and LEAST in Azure Synapse Analytics, as well as how they provide a more concise and efficient solution for developers compared to existing T-SQL alternatives.


 


Functionality


 


GREATEST and LEAST are scalar-valued functions and return the maximum and minimum value, respectively, of a list of one or more expressions.


 


The syntax is as follows:

GREATEST ( expression1 [ ,...expressionN ] )
LEAST ( expression1 [ ,...expressionN ] )

 


As an example, let’s say we have a table CustomerAccounts and wish to return the maximum account balance for each customer:


 



































CustomerID Checking Savings Brokerage
1001 $ 4,294.10 $ 14,109.84 $ 3,000.01
1002 $ 51,495.00 $ 97,103.43 $ 0.02
1003 $ 10,619.73 $ 33,194.01 $ 5,005.74
1004 $ 24,924.33 $ 203,100.52 $ 10,866.87

 


Prior to GREATEST and LEAST, we could achieve this through a searched CASE expression:

SELECT CustomerID, GreatestBalance =
    CASE
        WHEN Checking >= Savings and Checking >= Brokerage THEN Checking
        WHEN Savings > Checking and Savings > Brokerage THEN Savings
        WHEN Brokerage > Checking and Brokerage  > Savings THEN Brokerage
    END
FROM CustomerAccounts;

 


We could alternatively use CROSS APPLY

SELECT ca.CustomerID, MAX(T.Balance) as GreatestBalance
FROM CustomerAccounts as ca
CROSS APPLY (VALUES (ca.Checking),(ca.Savings),(ca.Brokerage)) AS T(Balance)
GROUP BY ca.CustomerID;

 


Other valid approaches include user-defined functions (UDFs) and subqueries with aggregates.


However, as the number of columns or expressions increases, so does the tedium of constructing these queries and the lack of readability and maintainability.


 


With GREATEST, we can return the same results as the queries above with the following syntax:

SELECT CustomerID, GREATEST(Checking, Savings, Brokerage) AS GreatestBalance 
FROM CustomerAccounts;

 


Here is the result set:

CustomerID  GreatestBalance
----------- ---------------------
1001            14109.84
1002            97103.43
1003            33194.01
1004           203100.52

(4 rows affected)

 


Similarly, if you previously wished to return a value that’s capped by a certain amount, you would need to write a statement such as:

DECLARE @Val INT = 75;
DECLARE @Cap INT = 50;
SELECT CASE WHEN @Val > @Cap THEN @Cap ELSE @Val END as CappedAmt;

 


With LEAST, you can achieve the same result with:

DECLARE @Val INT = 75;
DECLARE @Cap INT = 50;
SELECT LEAST(@Val, @Cap) as CappedAmt;

 


The syntax for an increasing number of expressions is vastly simpler and more concise with GREATEST and LEAST than with the manual alternatives mentioned above


As such, these functions allow developers to be more productive by avoiding the need to construct lengthy statements to simply find the maximum or minimum value in an expression list.


 


Common use cases


 


Constant arguments


One of the simpler use cases for GREATEST and LEAST is determining the maximum or minimum value from a list of constants:

SELECT LEAST ( '6.62', 33.1415, N'7' ) AS LeastVal;

 


Here is the result set. Note that the return type scale is determined by the scale of the highest precedence argument, in this case float.

LeastVal
--------
6.6200

(1 rows affected)

 


Local variables


 


Perhaps we wish to compare column values in a WHERE clause predicate against the maximum value of two local variables:

CREATE TABLE dbo.studies (
    VarX varchar(10) NOT NULL,
    Correlation decimal(4, 3) NULL
);

INSERT INTO dbo.studies VALUES ('Var1', 0.2), ('Var2', 0.825), ('Var3', 0.61);
GO

DECLARE @PredictionA DECIMAL(2,1) = 0.7;
DECLARE @PredictionB DECIMAL(3,2) = 0.65;

SELECT VarX, Correlation
FROM dbo.studies
WHERE Correlation > GREATEST(@PredictionA, @PredictionB);
GO

 


Here is the result set: 

VarX       Correlation
---------- -----------
Var2              .825

(1 rows affected)

 


Columns, constants and variables


 


At times we may want to compare columns, constants and variables together. Here is one such example using LEAST:

CREATE TABLE dbo.products (
    prod_id INT IDENTITY(1,1),
    listprice smallmoney NULL
);

INSERT INTO dbo.products VALUES (14.99), (49.99), (24.99);
GO

DECLARE @PriceX smallmoney = 19.99;

SELECT LEAST(listprice, 40, @PriceX) as LeastPrice
FROM dbo.products;
GO

 


And the result set:

LeastPrice
------------
     14.99
     19.99
     19.99

 


Summary


 


GREATEST and LEAST provide a concise way to determine the maximum and minimum value, respectively, of a list of expressions.


For full documentation of the functions, see GREATEST (Transact-SQL) – SQL Server | Microsoft Docs and LEAST (Transact-SQL) – SQL Server | Microsoft Docs.


These new T-SQL functions will increase your productivity and enhance your experience with Azure Synapse Analytics.


 


Providing the GREATEST developer experience in Azure is the LEAST we can do.


 


John Steen, Software Engineer
Austin SQL Team


Introducing the GREATEST and LEAST T-SQL functions

This article is contributed. See the original author and article here.


We are excited to announce that the GREATEST and LEAST T-SQL functions are now generally available in Azure SQL Database, as well as in Azure Synapse Analytics (serverless SQL pools only) and Azure SQL Managed Instance.


The functions will also be available in upcoming releases of SQL Server.


This post describes the functionality and common use cases of GREATEST and LEAST in Azure SQL Database, as well as how they can provide a more concise and efficient solution for developers compared to existing T-SQL alternatives.


 


Functionality


 


GREATEST and LEAST are scalar-valued functions and return the maximum and minimum value, respectively, of a list of one or more expressions.


 


The syntax is as follows:

GREATEST ( expression1 [ ,...expressionN ] )
LEAST ( expression1 [ ,...expressionN ] )

 


As an example, let’s say we have a table CustomerAccounts and wish to return the maximum account balance for each customer:


 



































CustomerID Checking Savings Brokerage
1001 $ 4,294.10 $ 14,109.84 $ 3,000.01
1002 $ 51,495.00 $ 97,103.43 $ 0.02
1003 $ 10,619.73 $ 33,194.01 $ 5,005.74
1004 $ 24,924.33 $ 203,100.52 $ 10,866.87

 


Prior to GREATEST and LEAST, we could achieve this through a searched CASE expression:

SELECT CustomerID, GreatestBalance =
    CASE
        WHEN Checking >= Savings and Checking >= Brokerage THEN Checking
        WHEN Savings > Checking and Savings > Brokerage THEN Savings
        WHEN Brokerage > Checking and Brokerage  > Savings THEN Brokerage
    END
FROM CustomerAccounts;

 


We could alternatively use CROSS APPLY

SELECT ca.CustomerID, MAX(T.Balance) as GreatestBalance
FROM CustomerAccounts as ca
CROSS APPLY (VALUES (ca.Checking),(ca.Savings),(ca.Brokerage)) AS T(Balance)
GROUP BY ca.CustomerID;

 


Other valid approaches include user-defined functions (UDFs) and subqueries with aggregates.


However, as the number of columns or expressions increases, so does the tedium of constructing these queries and the lack of readability and maintainability.


 


With GREATEST, we can return the same results as the queries above with the following syntax:

SELECT CustomerID, GREATEST(Checking, Savings, Brokerage) AS GreatestBalance 
FROM CustomerAccounts;

 


Here is the result set:

CustomerID  GreatestBalance
----------- ---------------------
1001            14109.84
1002            97103.43
1003            33194.01
1004           203100.52

(4 rows affected)

 


Similarly, if you previously wished to return a value that’s capped by a certain amount, you would need to write a statement such as:

DECLARE @Val INT = 75;
DECLARE @Cap INT = 50;
SELECT CASE WHEN @Val > @Cap THEN @Cap ELSE @Val END as CappedAmt;

 


With LEAST, you can achieve the same result with:

DECLARE @Val INT = 75;
DECLARE @Cap INT = 50;
SELECT LEAST(@Val, @Cap) as CappedAmt;

 


The syntax for an increasing number of expressions is vastly simpler and more concise with GREATEST and LEAST than with the manual alternatives mentioned above


As such, these functions allow developers to be more productive by avoiding the need to construct lengthy statements to simply find the maximum or minimum value in an expression list.


 


Common use cases


 


Constant arguments


One of the simpler use cases for GREATEST and LEAST is determining the maximum or minimum value from a list of constants:

SELECT LEAST ( '6.62', 33.1415, N'7' ) AS LeastVal;

 


Here is the result set. Note that the return type scale is determined by the scale of the highest precedence argument, in this case float.

LeastVal
--------
6.6200

(1 rows affected)

 


Local variables


 


Perhaps we wish to compare column values in a WHERE clause predicate against the maximum value of two local variables:

CREATE TABLE dbo.studies (
    VarX varchar(10) NOT NULL,
    Correlation decimal(4, 3) NULL
);

INSERT INTO dbo.studies VALUES ('Var1', 0.2), ('Var2', 0.825), ('Var3', 0.61);
GO

DECLARE @PredictionA DECIMAL(2,1) = 0.7;
DECLARE @PredictionB DECIMAL(3,2) = 0.65;

SELECT VarX, Correlation
FROM dbo.studies
WHERE Correlation > GREATEST(@PredictionA, @PredictionB);
GO

 


Here is the result set: 

VarX       Correlation
---------- -----------
Var2              .825

(1 rows affected)

 


Columns, constants and variables


 


At times we may want to compare columns, constants and variables together. Here is one such example using LEAST:

CREATE TABLE dbo.products (
    prod_id INT IDENTITY(1,1),
    listprice smallmoney NULL
);

INSERT INTO dbo.products VALUES (14.99), (49.99), (24.99);
GO

DECLARE @PriceX smallmoney = 19.99;

SELECT LEAST(listprice, 40, @PriceX) as LeastPrice
FROM dbo.products;
GO

 


And the result set:

LeastPrice
------------
     14.99
     19.99
     19.99

 


Summary


 


GREATEST and LEAST provide a concise way to determine the maximum and minimum value, respectively, of a list of expressions.


For full documentation of the functions, see GREATEST (Transact-SQL) – SQL Server | Microsoft Docs and LEAST (Transact-SQL) – SQL Server | Microsoft Docs.


These new T-SQL functions will increase your productivity and enhance your experience with Azure Synapse Analytics, Azure SQL Database, and Azure SQL Managed Instance.


 


Providing the GREATEST developer experience in Azure is the LEAST we can do.


 


John Steen, Software Engineer
Austin SQL Team


Experiencing Data Access issue in Azure Portal for Many Data Types – 04/22 – Investigating

This article is contributed. See the original author and article here.

Initial Update: Thursday, 22 April 2021 20:45 UTC

We are aware of issues within Application Insights and are actively investigating. Some customers may experience intermittent inaccurate or missing data within the Live Diagnostics (also known as Live Metrics) experience.
  • Work Around: None
  • Next Update: Before 04/23 00:00 UTC
We are working hard to resolve this issue and apologize for any inconvenience.
-Saika

Experiencing Latency and Data Loss issue in Azure Portal for Many Data Types – 04/22 – Investigating

This article is contributed. See the original author and article here.

Initial Update: Thursday, 22 April 2021 20:45 UTC

We are aware of issues within Application Insights and are actively investigating. Some customers may experience inaccurate or data gaps on Live Metrics Stream charts.
  • Work Around: None
  • Next Update: Before 04/23 00:00 UTC
We are working hard to resolve this issue and apologize for any inconvenience.
-Saika

[Guest Blog] A Journey of Firsts in Mixed Reality

[Guest Blog] A Journey of Firsts in Mixed Reality

This article is contributed. See the original author and article here.

This guest blog was written by Kimberly Castro, Program Manager in Microsoft’s Cloud + AI division. She shares about her career journey transitioning from the military into in the world of mixed reality as part of our Humans of Mixed Reality series.


 


I knew I wanted to be a part of Mixed Reality the first time I heard about it. I had just finished my interview loop with Microsoft when I heard that one more team wanted to talk to me before leaving. Shortly after, which actually felt like an eternity due to interview nerves, I was in a focus room with Miguel Sussffalich. After a few questions, he started telling me everything he could about the IVAS program.


 


Demos.jpg


 


Admittedly I was late to the game. I had never put on a VR/AR/MR headset, never heard of the HoloLens, and if I’m completely honest, I’d never used a Microsoft product aside from the Office Suite. You see, I grew up in an Apple household. You’re probably wondering how someone like me even managed to land an interview at Microsoft in the first place, especially coming from a rather non-traditional tech background.


 


After graduating from the University of Arizona in 2013, I enlisted in the Army as a Linguist. I spent the next two years learning Mandarin Chinese and eventually found myself in the Special Operations Command working Intelligence, Surveillance, and Reconnaissance (ISR) missions. After one more year, I started doing similar work at the NSA. At that point, I started seeing myself in a government contractor position once fulfilling my enlistment contract. It would’ve been an incredibly natural and easy transition to make. Not a whole year had passed when the Army said it was time for me to move again – my 7th move in 5 years. I was extremely frustrated to have to pack up my life and travel across the country again, but I had always wanted to spend time in the Pacific Northwest – and it ended up being my best move yet.



As the end of my contract was nearing, I received approval to participate in the Microsoft Software and Systems Academy (MSSA), a transition program for active duty military and veterans, which is essentially a C# bootcamp. Upon completing MSSA you are guaranteed a first-round interview in the form of a phone screening with Microsoft. I did well enough at the first round to be invited back to participate in a full interview loop.



I was sitting across from Miguel, learning about the IVAS program for the first time. He told me about a synthetic training environment where soldiers could practice clearing an enemy and a hostage-filled building safely and effectively. I was immediately transported back six years to basic training, where I first experienced the military’s Glass House training drill. Glass House is a generous term – in reality, it is just tape placed in a square on the ground, in a field, with a door-sized opening. “Rooms” were extended or added with more tape to alter the scenario, making an already limited training environment increasingly vague and less effective. You and your team members would have to imagine the walls, the roof, the enemy, the hostages, everything. The drill sergeants would yell out increasingly complex instructions, and eventually, your team would fail, reset, and try again. Of course, there was no way to know if you missed an enemy or pointed your weapon at a hostage or team member by accident, just whatever the drill sergeants could see. Additionally, with all the imagining that had to be done, it was impossible to realistically create the stress levels that an experience like that would entail in real-life. Essentially it wasn’t the best way to train, but it was all we had.


 


As soon as Miguel paused, I said, “Oh, this is going to save lives,” and from then on I was hooked.



I graduated from the MSSA program with offers from three different teams within Microsoft and an offer from another company. None of the other scopes of work came close to the experience I knew Mixed Reality was going to provide. So, in January of 2020, I joined the Delaware team as a program manager. I was just as excited as I was terrified. I was so sure people would quickly find out I was a fraud who tricked her way into Microsoft – over a year later, and I still have moments when I feel like this. I like to joke that it’s not imposter syndrome because I’m actually an imposter (I know, I know).



Within mere days of joining the team, I experienced my first hologram. A friend slapped a HoloLens 2 device on my head and showed me a bunch of cute animals dancing around. He explained that I could resize the figures and place them wherever I wanted in the physical space. I think I stayed in device for about an hour playing with everything I could; it felt like magic. I would’ve stayed for longer, but I suddenly remembered that I was in the middle of the dev space yelling out, “This is so cool!” every few minutes while people were working. To this day, one of my favorite things to do is run demonstrations to show off what is possible with Mixed Reality. I love seeing people experience the magic of their first hologram or surprising someone who has seen it all before by dropping them in the middle of New York City or the bottom of the Red Sea.



Another aspect I enjoy is customer focus and human-centered design. The infinite feedback loop and drive to figure out what someone needs, particularly when they themselves don’t know, really resonates with me. Accomplishing that gives me the same feeling as snapping the last puzzle piece into place; it’s a satisfying completion. This puzzle, however, is infinite.


 


The public sector has been the perfect place for me to dive in because I can really understand the user – after all, I used to be one. It’s incredibly comfortable to speak the same language as the customer, and I am so excited to have the opportunity to continue in this space. In the future, I would enjoy getting into the commercial and consumer or entertainment studios as well. I would love to help create something that furthers scientific research or builds something solely for a user’s enjoyment. Again, I love the public sector, it’s the perfect place for me to learn and grow, but I will want to take a step back from life and death situations one day.



It’s difficult to express how much I’ve learned over the last year and yet I know I’ve barely scratched the surface. I feel so privileged to work with artists, musicians, designers, and engineers every single day. I have never considered myself a creative type, but working in this space with these incredibly talented individuals has, for the first time, sparked a passion to create in my professional and personal life.


 


I hope you will discover an exciting new world with mixed reality too – the possibilities are endless!


 


#MixedReality #CareerJourneys