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

# Row calculations using a derived table

My Cross Tab query is:

```select count(*)                  as Total
, sum( case when Prov = 'AB'
then Quantity else 0 end ) as AB
, sum( case when Prov = 'BC'
then 1 else 0 end ) as BC
from mytable```

Is it possible to achieve an output like this?

```Total  AB  BC  Others
150   75  25   25```

Yes.

In your example, you sum Quantity for the AB count, but 1s and 0s for the BC count. Also, your example doesn't add up (150-75-25 does not equal 25). I shall assume these were typos, and just answer the issue of calculating the Others column.

One way to do it is to use another CASE expression:

```select count(*)                  as Total
, sum( case when Prov = 'AB'
then 1 else 0 end ) as AB
, sum( case when Prov = 'BC'
then 1 else 0 end ) as BC
, sum( case when Prov in ('AB','BC')
then 0 else 1 end ) as Others
from mytable```

To avoid the unnecessary extra CASE evaluation and summing, you can "reuse" the expressions already being calculated:

```select count(*)                  as Total
, sum( case when Prov = 'AB'
then 1 else 0 end ) as AB
, sum( case when Prov = 'BC'
then 1 else 0 end ) as BC
, count(*)
- sum( case when Prov = 'AB'
then 1 else 0 end )
- sum( case when Prov = 'BC'
then 1 else 0 end ) as Others
from mytable```

If the optimizer is any good, it will recognize those individual terms and not calculate them twice.

Some databases might allow you use a column alias elsewhere in the query after it's been defined, but I personally would not rely on this. So the above query would simplify to:

```select count(*)                  as Total
, sum( case when Prov = 'AB'
then 1 else 0 end ) as AB
, sum( case when Prov = 'BC'
then 1 else 0 end ) as BC
, Total-AB-BC   as Others
from mytable```

The slickest approach, which has the advantage that it avoids all unnecessary CASE evaluations and summing, is to use a derived table:

```select Total, AB, BC, Total-AB-BC as Others
from ( select count(*)               as Total
, sum( case when Prov = 'AB'
then 1 else 0 end ) as AB
, sum( case when Prov = 'BC'
then 1 else 0 end ) as BC
from mytable )```

Neat, eh?

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

• ### Amazon Quantum Ledger Database brings immutable transactions

Based on technology built internally at Amazon, the tech giant's newest database provides a centralized approach for enabling a ...

• ### Stibo Systems advances multidomain MDM system

The new Stibo Systems 9.2 update expands the MDM platform's features with Sisense business intelligence integration and machine ...

• ### Cloudera Data Platform to debut, as big data fortunes waver

The interim CEO of Cloudera is cautiously optimistic about growth prospects as the big data vendor acquired Arcadia Data and ...

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

• ### BI for mobile remains a challenge for vendors

While some BI vendors have developed effective mobile apps that provide concise insights, those that have attempted to recreate ...

• ### Magento BI update a benefit to vendor's e-commerce customers

Magento rolled out the Magento Business Intelligence Summer 2019 Release, updating its BI platform with enhanced scheduling ...

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

• ### Eclipse completes enterprise Java move with Jakarta EE 8

The Eclipse Foundation has assumed control of the Java EE enterprise platform and specifications from Oracle and has launched an ...

• ### AI development tools make software development easier

Software tools with AI can help optimize various elements to make the development pipeline easier to handle for all those ...

• ### Close Agile open source tools vulnerabilities

The State of the Software Supply Chain report highlights some developer team best practices to help increase productivity with ...

## SearchDataCenter

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

• ### Data center management as a service launches DCIM to the cloud

DMaaS is an option for organizations that want to use cloud-based management. Though these offerings hold a lot of promise, there...

• ### Decrease legacy hardware security gaps

Dated infrastructure poses security threats to organizations -- especially with the amount of sensitive data used today. Address ...

## SearchContentManagement

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

• ### Microsoft PowerApps pricing proposal puts users on edge

Microsoft's PowerApps pricing changes, expected to take effect in October, could make them too expensive for some users who ...

## SearchHRSoftware

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

• ### Before deploying recruitment chatbots, define use cases

HR departments can benefit from using chatbots for basic tasks, but the technology will eventually be able to handle more complex...

Close