23
Mon, Dec
3 New Articles

SQL 101: Aggregate Functions: Discovering SQL's Hidden Gems

SQL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

SQL's ability to make sense of large sets of data by using simple functions that allow you to aggregate data is a major tool that every programmer should master. Learn how to make the most of SQL's aggregate functions.

 

How do you calculate a total amount based on detail records in RPG? You probably read each record, add the respective amount to a work variable, and move on to the next record, until the file (or subset of records) ends. Well, in SQL you can do the same with a single instruction. Let's explore the awesome power and flexibility of SQL's aggregate functions, making use of what I explained before about the Select SQL instruction. In that article, I introduced an Inventory Master Table, which I have been using in most examples. This table's name is InvMst, and its description is presented in the table below:

 

The InvMst File Description

Column Name

Data Type

Length

Column Description

ItemID

Character

15,0

Item ID

LotNbr

Decimal

13,0

Lot Number

ExpDate

Date

N/A

Expiration Date

WHID

Decimal

8,0

Warehouse ID

ShelfID

Decimal

12,0

Shelf ID

ItemUn

Character

3

Item Units

ItemQty

Decimal

9,2

Item Quantity

 

Let's see, for instance, how to return the total quantity of the Item with ID 'A123' that exists in warehouse 24. SQL provides several types of functions you can usetogether with constants, operators, and column namesto create all sorts of expressions. In this case, I'll use a SUM function, which does the same as its MS Excel counterpart: returns the sum of a set of values. Here's how you use it to get the total quantity of Item 'A123' that exists in warehouse 24:

 

SELECT      ItemID

            , SUM(ItemQty)

FROM        InvMst

WHERE       WHID = 24

GROUP BY    ItemID

 

Notice that last line, GROUP BY ItemID? This is a requirement of some functions, even though you can use it whenever you want to group results, regardless of the functions you specified in the Select clause. The Group By reserved expression does what its name implies (allows you to group the results by one or more columns), and its structure is similar to Order By (it also expects one or more column names). It will aggregate the records retrieved that have those columns' contents in common. In this simple example, I'm grouping by ItemID and getting a single line as a result, but I could add a comma and "ShelfID" to the Group By expression, and the aggregation would be different: I'd get a line per each ItemID / ShelfID combination.

 

In abstract terms, an aggregate function, such as the SUM, takes a set of values (like a column of data) and returns a single value result from the set of values. This set of values will be determined by the WHERE and GROUP BY clauses of your SQL statement.

 

One of the most commonly used is the average function. Calculating an average in RPG is not very difficult, but it requires a loop and at least two variables; you read your data in a loop, sum up the values into a variable, and increase a counter (your second variable). In the end, you just divide the sum by the counter and voilá! The average is calculated.

 

In SQL, however, there's a function for that: AVG. Its syntax is similar to SUM; you just need to specify the field name for which you want to calculate the average enclosed in parentheses after the function name: AVG(<field name>). You can use it in all DML statements, even though I can't imagine how this can be used in a Delete statement. Anyway, here's an example: calculating the average quantity of item 'A123' per shelf in warehouse 333 is as simple as this:

 

SELECT            ItemID

            , AVG(ItemQty)

FROM        InvMst

WHERE       ItemID = 'A123'

            AND WHID = 333

GROUP BY    ItemID, ShelfID

 

I'm grouping by ShelfID to guarantee that my average is per shelf. If I hadn't specified the GROUP BY clause, the average would be calculated for the warehouse 333. AVG has two optional and mutually exclusive keywords, ALL and DISTINCT, which you can use to include all values or disregard the duplicates, respectively. By default, all values are considered. In other words, specifying ALL is redundant. However, there may be times in which it makes sense to use DISTINCT. Here's the average calculation from the previous example, disregarding the duplicate item quantities:

 

SELECT            ItemID

            , AVG(DISTINCT ItemQty)

FROM        InvMst

WHERE       ItemID = 'A123'

            AND WHID = 333

GROUP BY    ItemID, ShelfID

 

When you run this statement, it will return a rather ugly AVG column value. Assuming that the average is one hundred, the returned value is 100.000000000000000000000000, because of the way the average is internally calculated by the database engine. You have a few different ways to "beautify" the output, similar to RPG's %EDITC and %EDITW, but let's take the opportunity to introduce another useful SQL instruction: CAST. SQL can work magic, but it doesn't cast spells; it casts data types. CAST's syntax is simple:

 

CAST ( <expression> as <data type>).

 

The following example transforms the average column into a DECIMAL (9, 2) column:

 

SELECT            ItemID

, CAST(AVG(DISTINCT ItemQty) AS dec (9,2))

FROM        InvMst

WHERE       ItemID = 'A123'

AND WHID = 24

GROUP BY   ItemId, ShelfId

 

I'm highlighting the changes in bold. You can use CAST in all the places you'd use a column or expression. However, you need to use it carefully, because some CAST operations require some processing, which will cause the operation to take longer.

 

If I wanted to count the shelves that contain the 'A123' item instead of calculating their average quantity, SQL also provides a simple to use function for that: COUNT. The syntax is similar to AVG in every way, right down to the ALL and DISTINCT keywords. The difference is, other than the obvious operation that is executed, you can use COUNT for any type of expression, while AVG only works with numeric expressions or fields. Here's a simple count scenario: I want to count how many shelves in warehouse 333 contain item 'A123':

 

SELECT            ItemID

            , COUNT(ShelfID)

FROM        InvMst

WHERE       ItemID = 'A123'

            AND WHID = 333

GROUP BY    ItemID

 

Here, the DISTINCT keyword is more relevant, because in a COUNT it might make sense to eliminate the duplicates. COUNT is commonly used to return the number of rows in a table; again using the InvMst table as an example, I'd use the following to calculate how many records InvMst has:

 

SELECT            COUNT(*)

FROM        InvMst

 

Note that COUNT returns a DECIMAL(15, 0) field; you need to use COUNT_BIG when the expected result is greater than the maximum value of an integer. COUNT_BIG works exactly like COUNT, with the difference that it can return a DECIMAL(31, 0) value as maximum output.

 

Some other times, you just need the maximum or minimum of a set of values. You can use MAX and MIN, respectively, to get those results. These two functions are similar to COUNT in every way, but using the DISTINCT keyword has no practical effect. If I wanted to return the minimum and maximum expiration dates of item 'A123' in warehouse 333, regardless of the shelf it sits in, I'd use the following statement:

 

SELECT            MIN(ItemQty)

            , MAX(ItemQty)

FROM        InvMst

WHERE       WHID = 333

 

As you can see from this example, you can use multiple column functions in the same statement. However, due to their specific nature, some are not compatible with each other and can't be used together.

 

Now that you're getting the hang of it, the next articles will discuss the scalar functions. Don't know what they are? Well, here's a sneak peek of what's coming: a scalar function takes input arguments and returns a single value result. The restrictions on the use of aggregate functions do not apply to scalar functions, because a scalar function is applied to single parameter values rather than to sets of values.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: