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

# Two ways to SUM a complex expression

## From the given table I want to derive the count for OOS as a separate field (CntOOS) that gives me the number of times 'OOS' appeared in the above three visits.

I have a table like follows:

```------- Existing fields -------| Derive
client  Visit1  Visit2  Visit3 | CntOOS
---------------------------------------
A         OOS    OOS     RTN   |    2
B         RTN    OOS     D&D   |    1
C                RTN           |    0
D         RTN    OOS     D&D   |    1```

From the given table I want to derive the count for OOS as a separate field (CntOOS) that gives me the number of times 'OOS' appeared in the above three visits.

You can do this easily with CASE expressions:

```select client
, case when Visit1 = 'OOS'
then 1 else 0 end
+ case when Visit2 = 'OOS'
then 1 else 0 end
+ case when Visit3 = 'OOS'
then 1 else 0 end   as CntOOS
from yourtable```

You did not ask this, but if you need a total of CntOOS for all clients, there are two ways to do it. The first is to wrap the SUM function around the complex CntOOS expression:

```select sum(
, case when Visit1 = 'OOS'
then 1 else 0 end
+ case when Visit2 = 'OOS'
then 1 else 0 end
+ case when Visit3 = 'OOS'
then 1 else 0 end
) as sum_CntOOS
from yourtable```

The second way is to wrap another query around your original query:

```select sum(CntOOS) as sum_CntOOS
from (
select client
, case when Visit1 = 'OOS'
then 1 else 0 end
+ case when Visit2 = 'OOS'
then 1 else 0 end
+ case when Visit3 = 'OOS'
then 1 else 0 end   as CntOOS
from yourtable
) as details```

Sometimes it's good to have alternative ways of doing something in SQL. The second method utilizes a derived table, also known as an inline view.

#### 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

• ### PostgreSQL 12 boosts open source database performance

Widely used open source PostgreSQL database platform gets a major update providing users with new SQL query capabilities for JSON...

• ### Data integration vs. ETL in the age of big data

Data integration provides a consistent view of business performance across multiple data sources, though it needs to keep up with...

• ### 5 steps to an improved data quality assurance plan

Consultant David Loshin offers tips on developing a data quality strategy that can help identify data errors before they cause ...

• ### ThoughtSpot 6 advances AI power of BI platform

New augmented intelligence and machine learning tools, along with a new mobile app, highlight the release of the new update to ...

• ### Promethium tool taps natural language processing for analytics

The software, called Data Navigation System, was designed to enable non-technical users to make complex SQL requests using plain ...

• ### Intelligence as a service is key to nonprofit's urban planning

Cobalt Community Research, a nonprofit organization based in Michigan, uses data collected and packaged by TruFactor for urban ...

## SearchSAP

• ### CEO of SAP Bill McDermott abruptly resigns

Bill McDermott, CEO of longtime ERP, CRM and BI software vendor SAP, resigned Thursday triggering a succession plan that promotes...

• ### What is an SAP Intelligent Enterprise? Don't look for a clear answer

What does Intelligent Enterprise actually mean? If the SAP term confuses you, you're not alone. Learn what the experts say and ...

• ### Pros and cons of SAP migration to the cloud

Hosting SAP on cloud services can bring added security and scalability. But organizations must also watch out for issues such as ...

## 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

• ### Pivotal, Microsoft team up to deliver Azure Spring Cloud

Azure Spring Cloud, jointly developed by Microsoft and Pivotal, lets Spring developers bring apps to the cloud without concern ...

• ### GitHub tempts enterprises with Semmle, security enhancements

With the Semmle semantic code analysis engine freshly added to its quiver, GitHub gives corporate development teams one way to ...

• ### You need more than web app security to stop API attacks

API and web application vulnerabilities may share some common traits, but it's where they differ that hackers will target.

## SearchDataCenter

• ### IBM quantum computers' usefulness in sight -- using binoculars

IBM's Bob Sutor discusses Big Blue's new quantum systems and computation center, the realities of quantum computing today and how...

• ### Addendum sets ASHRAE 90.4 as energy-efficiency standard

The publication of ASHRAE 90.4 in 2016 brought a new set of energy guidelines, but the industry still used Standard 90.1. A new ...

• ### How to realize the benefits of software-defined infrastructure

Software-based infrastructure and services can streamline management and application data. Before you add them to your data ...

## SearchContentManagement

• ### Digital asset management benefits the future of work

Digital asset management promises to transform digital work, making it easier to tag documents, analyze video and provide image ...

• ### Box cloud content management adds PDF tools, boosts security

Box partners with Adobe in releasing a new set of PDF tools that can be used within Box's content management cloud to enable ...

• ### AI optimizes content management workflows, personalizes CX

AI is poised to revolutionize content management workflows, bringing order to enterprise documents, greater insights into ...

## SearchHRSoftware

• ### Get smarter about HR data security

HR deals with mountains of sensitive employee data and must do a better job of protecting that information. Here's a look at how ...

• ### Walmart, FedEx make case for VR training

The payback from virtual training may be improved retention and shorter training times. An hour-long training period can be ...

• ### Do you know how to improve employee experience? Test yourself

Think you know what it takes to create a positive employee experience for your workers? Take this EX quiz to test your savvy and ...

Close