Q

# Calculate amount percentages

## I have a table with two columns, ID and amount. I need to calculate percentage amount allocated to an ID.

I have a table with two columns, ID and amount. I need to calculate percentage amount allocated to an ID.

It would be nice if we could simply say:

```select id
, pct(amount) as pct_amt
from daTable
group
by id```

Unfortunately, there is no such PCT function. However, we can take a cue from a similar function, AVG. To calculate the ID averages, we can do this:

```select id
, avg(amount) as avg_amt
from daTable
group
by id```

But we can also calculate the averages this way:

```select id
, sum(amount)
/ count(*) as avg_amt
from daTable
group
by id```

In the above query, for each ID, we divide the total amount for that ID by the number of rows for that ID, to obtain the average.

In a similar way, we can calculate a percentage:

```select id
, ( 100.0 * sum(amount) )
/ ( select sum(amount)
from daTable ) as pct_amt
from daTable
group
by id```

In this query, we divide the total amount for each ID by the total amount for all IDs, i.e., the total for the entire table. Note that the sum(amount) in the dividend (the number being divided, i.e. on the top of the division) is for each individual ID, while the sum(amount) in the divisor (the dividing number, i.e. on the bottom of the division) is for all IDs. The divisor is a non-correlated subquery, which means that it can be evaluated once by the optimizer before the query starts, and then used in the calculation for each ID.

In the calculation of a percentage, we multiply the division by 100.0 to give the desired percentage number, and we sneak this into the calculation before the division, to ensure we get an accurate result. This is because sometimes the SUMs involved are integers, and division of one integer by another integer results in an integer answer, which means rounding. When we multiply the dividend by 100.0 first, this gives an intermediate decimal result, and then the division will yield a decimal number without rounding.

This was last published in September 2005

## Content

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

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

• ### With better scaling, semantic technology knocks on enterprise's door

Cambridge Semantics CTO Sean Martin says better scalability can lead to richer representations of data. Such advances are behind ...

• ### Big data management and analytics weather tumult -- with more in store

Cloud had a big impact on big data management and analytics last year. Machine learning and streaming designs will contribute to ...

• ### Embedded analytics to feel widest impact of machine learning projects

Ovum analyst Tony Baer discusses machine learning tools, IoT-driven streaming analytics and Hadoop in the cloud, all of which ...

• ### Researchers work on AI algorithms to detect fake news

A new challenge to identify fake news will test the boundaries of AI technology and offer a proving ground for innovative new ...

• ### Don't let a data-driven approach ax judgment from analytics equation

Data analytics can help improve decision-making in organizations. But human intuition and judgment need to be part of the picture...

• ### Lack of skills remains one of the biggest data science challenges

Many enterprises are struggling with the complexity of today's big data and data science ecosystem, though they recognize the ...

## SearchSAP

• ### SAP futurists name five future technology trends to plan for now

Three SAP futurists explain what future technology trends in augmented reality, blockchain, AI, robotics, and contingent labor ...

• ### Abakus acquisition beefs up SAP Hybris marketing portfolio

SAP acquires Abakus marketing measurement software for SAP Hybris marketing suite; openSAP adds new courses for S/4HANA, data ...

• ### The five best ways to manage SuccessFactors support after go-live

SuccessFactors skills are scarce in the job market, but you can manage a new system with a few organizational moves and a solid ...

## SearchSQLServer

• ### Four trends that will impact SQL Server DBAs in 2017

Flash storage adoption, cloud computing's growth, Linux's increased importance and broader big data integration are a few trends ...

• ### DATEADD and DATEDIFF SQL functions for datetime values

DATEADD and DATEDIFF SQL functions allow you to easily perform calculations, like adding a time interval from a datetime value. ...

• ### SQL Server on Linux signals Microsoft's changing development landscape

Expert Joey D'Antoni explains what SQL Server on Linux and the addition of some Enterprise Edition features to the database's ...

## TheServerSide

• ### Is JSON and XML your REST performance bottleneck?

Learn how ASCII encoding formats like JSON and REST can adversely impact server application performance compared with emerging ...

• ### Making multi-cloud deployment a reality at Netflix with Spinnaker

Learn how Netflix leveraged automation and Spinnaker to perform 4,000 multi-cloud deployments per day.

• ### The benefits of Java microservices in a Docker and DevOps world

To seasoned developers, microservices may sound like SOA by another name. But Java microservices apps delivered via Docker ...

## SearchDataCenter

• ### What should my dimensions be for a data center building design?

Don't get hung up on data center dimensions in the design phase. Focus on length-to-width ratios to get the most out of data ...

• ### Use a Linux file system journal for data integrity, performance

Understand the three different file system journaling modes for Linux, as well as which mount option provides the best levels of ...

• ### Test yourself on modern data center networking architecture

Evolving business needs demand a new approach to data center networking. Keep yourself updated on SDN, VXLAN and other ...

## SearchContentManagement

• ### Using Microsoft MyAnalytics to improve workers' time management

Microsoft MyAnalytics has been hailed as the next great thing for analyzing worker productivity. But how can employees be assured...

• ### Is the Drupal content management system right for your business?

The open source Drupal content management system offers users responsive design and WYSIWYG content and web development tools, as...

• ### Manage your content with the IBM Digital Experience WCM platform

IBM Digital Experience is a WCM platform that offers users features for creating and managing digital content that can be ...

## SearchFinancialApplications

• ### HR to face sea change in online learning software and accent on teams

Deloitte predicts far-reaching trends for HR, including innovations in learning, an expansion of artificial intelligence, and an ...

• ### Experts lack consensus on discrimination risks of video interviewing

When used too early in the hiring process, video can lead to discrimination based on race, age or sex, but it can have positive ...

• ### Five frontrunners named in enterprise performance management software

Oracle, Anaplan, Tagetik, IBM and Adaptive Insights lead the pack in an evaluation of EPM software that examines functional ...

Close