Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Averages over a span of years -- Part 2

In  Part 1 of this answer we examined averages produced by the AVG function on the following table:

```subject | year | enrolled ----------+---------+------------- subject1 | 1998 | 20 subject1 | 1999 | 23 subject1 | 2000 | 16 subject2 | 1999 | 10 subject2 | 2000 | 21 subject3 | 2000 | 9
```

The averages for each subject were the same whether we supplied the missing years or not. Here we'll explore why, and how to work with NULLs and aggregates.

When we supplied the missing years and found that averages were not affected, we demonstrated that aggregate functions exclude NULLs. The average was calculated using the number of non-NULL values in each subject group.

To get averages calculated over all years in the given span, where enrollment rows are missing, we must assume that the number enrolled was zero. This may not be a valid assumption in all applications. Remember, NULL is not equal to any value, and in particular, NULL is not equal to zero, so we have to do something specific to make it work that way.

The obvious solution is to check when the column is NULL, and use zero instead, which is completely in line with the assumption we are making and is the best way to solve the problem. We simply use the COALESCE function:

```select allyears.subject , avg(enrolled) as avgamt , avg(coalesce(enrolled,0)) as avgamtzero from ( select distinct subject , 1998+i as theyear from integers , subjects where i between 0 and 2 ) as allyears left outer join subjects on allyears.subject = subjects.subject and allyears.theyear = subjects.theyear group by allyears.subject
subject avgamt avgamtzero subject1 19.67 19.67 subject2 15.50 10.33 subject3 9.00 3.00
```

COALESCE is a standard SQL function. If your database does not support it, look for an equivalent function like ISNULL or NVL.

We made the correct calculation over the span of years by generating rows that were missing. Was this the easiest way? Consider the following query:

```select allyears.subject , sum(enrolled) as sumamt , count(enrolled) as countamt , count(*) as countrows , avg(enrolled) as avgamt , avg(coalesce(enrolled,0)) as avgamtzero from ( select distinct subject , 1998+i as theyear from integers , subjects where i between 0 and 2 ) as allyears left outer join subjects on allyears.subject = subjects.subject and allyears.theyear = subjects.theyear group by allyears.subject
subject sumamt countamt countrows avgamt avgamtzero subject1 59 3 3 19.67 19.67 subject2 31 2 3 15.50 10.33 subject3 9 1 3 9.00 3.00
```

Notice that COUNT(*) counts rows, whether any particular column had NULLs or not. COUNT(*) ignores NULLs. This is the only exception to the rule that aggregate functions exclude NULLs, because COUNT(*) does not even look at columns.

So the other way of getting the desired averages is:

``` sum(enrolled) / count(*) as avgamtzero
```

I can't say whether this method is as efficient as AVG(COALESCE(xxx)), but my guess is that they are approximately the same.

Finally, there is one other approach. Instead of generating rows that were missing with a cross join to the integers table, just do this:

```select subject , sum(enrolled) / 3 as avgamt from subjects group by subject
subject avgamt subject1 19.67 subject2 10.33 subject3 3.00
```

Is this reasonable? Can we just plug 3 into the calculation for the range of years desired? The derived table subquery was fashioned using inspection to determine the range of years. In general, it's not that easy. The generation of missing rows using the cross join would have used additional subqueries to determine the first and last years, so that inspection would not be necessary. If you can do it by inspection, okay, but it's nice to know how to attack the general problem, too.

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Dremio Data Lake Engine 4.0 accelerates query performance

Dremio issues a new platform update, defining itself as data lake engine technology that looks to help users connect and query ...

• ### Weighing the use of third-party database administration tools

Database expert Chris Foot details the key reasons why DBAs should consider using third-party database administration to fill ...

• ### InfluxDB Cloud 2.0 expands open source time series database

While the core open source InfluxDB 2.0 project is still in alpha, InfluxData has advanced its cloud service to support new ...

• ### SAP BI platform stays strong due to cloud-based architecture

A cloud-native BI platform along with domain-specific applications that can be embedded to serve the needs of various industries ...

• ### Tableau 2019.3 highlighted by enhanced AI, new data catalog

Tableau released its third-quarter update Wednesday, featuring Explain Data, an enhanced augmented intelligence tool, and Tableau...

• ### Oracle BI platform on the comeback trail

Time had seemingly left Oracle's business intelligence tools behind -- until the vendor responded by consolidating its BI ...

## SearchSAP

• ### Navy sails SAP ERP systems to AWS GovCloud

The Navy consolidated 26 various ERP systems onto SAP HANA on the AWS GovCloud, leading to data reduction, lower TCO, and setting...

• ### SAP HANA application collects concussion data for real-time analysis

Protecht, an SAP HANA based system, enables teams in contact sports to measure and analyze data about hits though an IoT sensor ...

• ### S/4HANA public cloud vs. private cloud: Compare the pros, cons

In addition to SAP's on-premises and hybrid models, the vendor offers S/4HANA in public and private clouds. Here's a look at the ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Guide to practicing cloud-native development

In this guide, learn about cloud-native development -- what it is, how vendors try to exploit it and how to bring it to life with...

• ### Eclipse launches Che 7 IDE for Kubernetes development

The Eclipse Foundation has introduced Eclipse Che 7, a new version of the Eclipse Che cloud-based integrated development ...

• ### Java's popularity hinges on future developers, adaptability

Java needs to focus on the youth, adapt to emerging tech and constantly evaluate its performance to remain popular among ...

## SearchDataCenter

• ### New Dell EPYC servers embrace AMD Rome chips

Dell EMC goes from Naples to Rome with a new line of EPYC servers, including Ready Solutions for high-performance computing and ...

• ### Can next-gen SIEM help cybersecurity initiatives?

More organizations are using SIEM, AI and cloud technology to minimize security breaches. Though despite interest, this ...

• ### IBM z15 mainframe secures data across multi-cloud environments

IBM unveiled the latest in its line of mainframes capable of processing 1 trillion web transactions a day. The IBM z15 ...

## SearchContentManagement

• ### Nintex launches no-code workflow automation tool

Nintex has launched a no-code workflow automation tool, Workflow Generator, in an effort to bridge the gap between IT departments...

• ### Cohesion is latest Acquia acquisition

While Acquia would not provide specifics of the deal, the decision behind the acquisition of Cohesion was the chance to bring ...

• ### 3 things to learn before considering blockchain integration

Paul Swider, CTO at RealActivity LLC, discusses how to use blockchain technology to increase the security and usefulness of ...

## SearchHRSoftware

• ### SuccessFactors customers to see big Qualtrics impact

SuccessFactors is rethinking its HCM suite as it works to integrate Qualtrics into its applications. But HR managers are ...

• ### HiQ Labs vs LinkedIn case OKs robot monitoring of employees

A U.S. Court of Appeals ruled on the LinkedIn vs HiQ lawsuit, which could help shape how services gather information on social ...

• ### MyPayrollHR collapse stirs allegations, questions, anger

The problems created by the closing of MyPayrollHR may take a long time to resolve. The FBI has been contacted and lawsuits are ...

Close