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

# Getting a count of zero for unmatched rows

I have a reference table and a data table and wish to perform a GROUP BY. The thing I am trying to accomplish is the pull EVERY 'reference' from the reference table and count the rows from the data table, respectively. Of course I can simply perform the GROUP BY on the data table and get the stats. But this only gives me existing data counts. I was hoping to build a single query that gives counts, including those references with counts of zero. I believe this should be possible. Hopefully the question is clear.

The question is very clear. Don't feel bad, because everybody stumbles over this problem the first time.

The situation can be illustrated with the following sample data:

```References
ID Reference
1  HTML
2  CSS
3  PHP
4  SQL

Data
ID RefID
24  1
24  2
24  4
27  2
27  4
31  1
31  4```

In order to understand how to get the correct counts, let's first write a simple LEFT OUTER JOIN and show the results:

```select References.ID
, References.Reference
, Data.ID     as Data_ID
from References
left outer
join Data
on References.ID = Data.RefID
order
by References.ID
, Data.ID

ID Reference Data_ID
1  HTML      24
1  HTML      31
2  CSS       24
2  CSS       27
3  PHP       null
4  SQL       24
4  SQL       27
4  SQL       31 ```

Notice the row in the result set for PHP. This row shows up in a LEFT OUTER JOIN, but would be eliminated in an INNER JOIN. (Make sure you understand why.)

Now let's add the GROUP BY and the COUNT() function.

```select References.ID
, References.Reference
, count(*)     as Data_rows
from References
left outer
join Data
on References.ID = Data.RefID
group
by References.ID
, Data.ID
order
by References.ID

ID Reference Data_rows
1  HTML      2
2  CSS       2
3  PHP       1
4  SQL       3 ```

Wait a minute, that's wrong! We know there are no rows for PHP, but the count is showing 1. Why is this?

The answer is: COUNT(*) counts rows. There is a row for PHP in the result set.

What you have to do is count the number of Data.RefID values. Note that in the result set, the value of this column for the PHP row is NULL. And as you know, aggregate functions like COUNT() and SUM() ignore nulls.

```select References.ID
, References.Reference
, count(Data.RefID)     as Data_rows
from References
left outer
join Data
on References.ID = Data.RefID
group
by References.ID
, Data.ID
order
by References.ID

ID Reference Data_rows
1  HTML      2
2  CSS       2
3  PHP       0
4  SQL       3 ```

See the difference?

This was last published in May 2005

## Content

Find more PRO+ content and other member only offers, here.

#### Have a question for an expert?

Please add a title for your question

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

• ### Streaming tool from StreamSets eyes data in motion for GDPR

StreamSets software for inspecting big data brings governance to data in motion. Such capabilities may find more use as the ...

• ### Data expert: GDPR deadline is an opportunity, not a burden

There is stress as the EU's General Data Protection Regulation compliance deadline nears, but the GDPR privacy movement is a good...

• ### Google TPUs open up on cloud; LinkedIn intros Hadoop Dynamometer

In big data news, we find Google TPUs, or Tensor Processing Units, offered as a cloud service, while LinkedIn is open sourcing a ...

## SearchBusinessAnalytics

• ### Rethinking analytics processes spurs enterprise innovation

By taking a fresh look at the makeup of their analytics organizations, enterprises can innovate their business models and take ...

• ### Diversified data sets for analytics deliver top results

Analytics teams should focus on data diversity to ensure that their projects deliver the most meaningful insights -- but they ...

• ### How to boost the value of BI in today's analytics landscape

Traditional BI reporting still gives businesses valuable information. But its value can be increased by incorporating it into a ...

## SearchSAP

• ### SAP Ariba Live focuses on procurement for purpose

SAP Ariba Live 2018 focused on the idea that businesses can use procurement technology to do good in the world; for example, by ...

• ### SAP debuts consumption-based pricing model for SAP Cloud

SAP Cloud Platform is now available as a consumption-based model, an alternative to the subscription model. SAP also updated the ...

• ### SAP buys lead-to-money leader CallidusCloud to take on Salesforce

SAP paid \$2.4 billion to acquire lead-to-money vendor CallidusCloud, and analysts agree that the significant price may be worth ...

## SearchSQLServer

• ### Microsoft SQL Operations Studio eases SQL Server admin tasks

SQL Operations Studio simplifies routine administration of SQL Server and Azure SQL databases, making database development and ...

• ### Meltdown and Spectre fixes eyed for SQL Server performance issues

Microsoft has responded to the Spectre and Meltdown chip vulnerabilities with patches and other fixes. But IT teams need to sort ...

• ### Five SQL Server maintenance steps you should take -- ASAP

Putting off SQL Server administration tasks can lead to database problems. Enact these often-neglected maintenance items to help ...

## TheServerSide.com

• ### Stay ahead of Java security issues like SQL and LDAP injections

Java security best practices include validating input to prevent injection attacks and managing malicious JAR vulnerabilities.

• ### Why the Waterfall or Agile debate will be around forever

Which is the right methodology to use for your project: Waterfall or Agile? The industry may be at peak Agile, as the ...

• ### Chef's InSpec 2.0 brings compliance automation to the cloud

Enterprises have been quick to adopt automation tools for development and deployment but only recently have organizations started...

## SearchDataCenter

• ### IBM cloud services to secure mainframes out to the edge

Big Blue will introduce IBM cloud services that use blockchain, containers and its z14 mainframes to deliver improved security ...

• ### Four disadvantages of hyper-converged infrastructure systems

Problems with scalability and unexpected licensing costs can create problems for organizations that deploy hyper-converged ...

• ### IBM Power9 servers seek market inroads to AI, cloud

IBM follows up its first Power9 server with a raft of systems designed to appeal to a wider array of markets -- most notably, AI ...

## SearchContentManagement

• ### Scrivito unveils serverless CMS product

By building the CMS with ReactJS, Scrivito gained attraction with development community, according to an analyst.

• ### Content personalization tools sharpen focus on customers

Content personalization isn't new; Amazon weaponized it, and Jeff Bezos is the world's richest man. New tools are putting it ...

• ### Leading brands see the need for personalized content

Content personalization continues to expand within companies as maturing technologies make it a viable marketing option for ...

## SearchHRSoftware

• ### Blockchain in HR remains in beta, outlook uncertain

Blockchain in HR verification is getting interest from universities, vendors and startups. There are powerful forces driving its ...

• ### How people analytics can improve HR effectiveness

Getting insight into your workforce can reveal everything from training issues to the reasons for turnover or missed corporate ...

• ### At Ceridian, role of CIO requires constant learning, adjusting

You might say Warren Perlman, CIO at Ceridian, a global HCM software company, has been preparing for the role of CIO all his life...

Close