sqlschool.gr logo

articles

Articles of SQLschool.gr Team

GREATEST() and LEAST() functions in Azure SQL Database

Antonios Chatzipavlis
Tuesday 16 March 2021

Μια απαίτηση αρκετών χρόνων υλοποιήθηκε μερικώς στον SQL Server καθώς αυτή την στιγμή είναι μόνο διαθέσιμη στις Azure SQL Databases και αφορά δύο T-SQL functions που υπάρχουν σε άλλα RDBMS εδώ και χρόνια.

Η απαίτηση αυτή αφορά τις GEATEST και LEAST functions.

Η πρώτη επιστρέφει την μεγαλύτερη τιμή από τις τιμές που εισάγονται στις παραμέτρους της και δεύτερη την μικρότερη τιμή.

Ακόμα το documentation δεν είναι ενημερωμένο με αυτές αλλά η σύνταξη τους είναι αρκετά απλή.

Syntax

SELECT GREATEST(<value1>,<value2>…<valueN>);
SELECT LEAST(<value1>,<value2>…<valueN>);

Η προσθήκη αυτή κάνει αρκετά καλύτερη την συγγραφή των queries καθώς στο παρελθόν (αλλά ακόμα και τώρα όσον αφορά τον SQL Server) έπρεπε να καταφύγουμε σε διάφορες τεχνικές όπως αυτή που παρουσιάζω στο παράδειγμα παρακάτω με την χρήση του APPLY operator

Mimic GREATEST and LEAST function

select    
    o.orderid, 
    o.orderdate,
    o.requireddate,
    o.shippeddate,
    min(dd.dates) as min_date,
    max(dd.dates) as max_date
from 
    sales.Orders as o
outer apply 
    ( values (o.orderdate), (o.requireddate), (o.shippeddate) ) as dd(dates)
group by 
    o.orderid,
    o.orderdate, 
    o.requireddate,
    o.shippeddate;

Αντί για αυτό πλέον (ακόμα μόνο σε Azure SQL Databases) το παραπάνω query γίνεται όπως παρακάτω.

GREATEST and LEAST function usage

select    
    o.orderid, 
    o.orderdate,
    o.requireddate,
    o.shippeddate,
    LEAST(o.orderdate, o.requireddate, o.shippeddate) as min_date,
    GREATEST(o.orderdate, o.requireddate, o.shippeddate) as max_date
from 
    sales.Orders as o;

Θεωρώ ότι σύντομα θα είναι διαθέσιμες και στο SQL Server.


//Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios Chatzipavlis is a highly experienced Data Solutions Consultant and Trainer. He has been working in the IT industry since 1988, holding various roles such as senior developer, IT Manager, Data & AI Solutions Architect and Consultant.

Since 1995, Antonios has focused on modern technologies and software development tools, primarily by Microsoft. He has specialized in Data & AI since 2000, with expertise in Microsoft Data Platform (SQL Server, Azure SQL Databases, Azure Synapse Analytics, Microsoft Fabric, Power BI, AI) and Databricks.

Antonios is also a Microsoft Certified Trainer (MCT) for over 25 years, has been recognized as a Microsoft Most Valuable Professional (MVP) in Data Platform since 2010 and he is in the Data Expert 40 Powerlist 2024 by Boussias. He is the co-founder and visionary behind XLYTiCA, a company dedicated to Data & AI solutions.

Episode

Task Flows in Microsoft Fabric

image

More Episodes...

Tip

Get Certified: Become a Fabric Data Engineer

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn

sqlschool.gr © 2010-2025 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.