Q

# Showing results as a percentage

With a table with data like this:

```a
a
a
b
b
c
```
How can I show these results as a percentage (i.e count a / total records):
```a 50
b 33.333
c 16.666
```
I know a GROUP BY must be there. I want to do this in one query.

I'll try to reproduce your scenario. Here's the SQL to create and populate the table:

```create table MyTable
( MyColumn char(1) );

insert into MyTable values ('a');
insert into MyTable values ('a');
insert into MyTable values ('a');
insert into MyTable values ('b');
insert into MyTable values ('b');
insert into MyTable values ('c');
```
I came up with two different yet similar solutions. The first involves using a scalar subquery in the SELECT clause to obtain the total count. Here's the SQL:
```select MyColumn, round( 100.0 * count(*) /
( select count(*)
from MyTable )
,3 ) Perc
from MyTable
group by MyColumn;
```
The second solution involves a cross join with the same subquery. Because cross joins are the SQL equivalent to multiplication, and we know that the subquery returns one and only one row, then we know that the cross join will return one row per row in MyTable. Mathematically, this is the equivalent of saying, "X multiplied by 1 equals X." Grouping by T.CNT is required in order to make the join work, but this doesn't change the outcome because we know that T.CNT is constant for the entire query. And we know that having a secondary sort by a constant won't affect order.
```select MyColumn, round(100.0 * count(*) / t.cnt,3) Perc
from MyTable
cross join
( select count(*) cnt from MyTable ) t
group by MyColumn, t.cnt;
```
Both queries give the same result set:
```MYCOLUMN       PERC
-------- ----------
a                50
b            33.333
c            16.667
```
Which query you choose to use might depend on your particular SQL vendor's implementation, performance and/or which one you like better.

• The Best SQL Web Links: tips, tutorials, scripts, and more.
• Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
• Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.

This was last published in March 2002

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

• ### Cross-platform integration, data preparation process grows in cloud

While cloud computing may be convenient and more cost-effective for users, it can also lead to new challenges and requirements in...

• ### Graph data model cements tight relationships between data elements

Graph databases can help define and discover relationships between entities -- and offer increased flexibility and better ...

• ### Microsoft SQL Server 2016 relational DBMS overview

Microsoft SQL Server 2016 for Windows comes in four editions, with updates that include a new stretch database feature, Polybase,...

• ### Creative projects leave people guessing about future impact of AI

A push is underway to write creative AI algorithms that can engage in music, film and design projects. So far, they have ...

• ### Data visualization plays an important role in a big data environment

Enterprises are increasingly putting data visualization tools, like Tableau, at the top of their big data platforms to start ...

## SearchSAP

• ### Does SAP ONE Support Launchpad make SAP support any easier to use?

The new Fiori user experience makes it easier to access applications and support services, but product-specific support still ...

• ### At SME Summit, SAP Anywhere growing pains revealed

Attendees at an SAP SME Summit lauded the e-commerce front end, but some said back-end integration and payment-processing ...

• ### Planning a HANA big data strategy with SAP HANA Vora

SAP has worked hard to position HANA as a big data platform. To formulate a viable big data strategy, you need to know the tools,...

## SearchSQLServer

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

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

• ### How to get the most out of virtual SQL Server with Microsoft Hyper-V

SQL Server is a CPU-intensive technology, which can make it tricky to run in a virtualized environment. Keep your SQL Server ...

• ### Microsoft previews SQL Server on Linux, opens features across editions

Microsoft looks to broaden the horizons of SQL Server, as it moves some Enterprise features to Standard Edition and issues the ...

## TheServerSide

• ### Docker instances become the new norm and adoption goes mainstream

Many organizations use Docker instances for many reasons, although security, data storage and monolithic fears remain barriers to...

• ### How to turn your DevOps failures into ALM successes

Doing the right thing doesn't always mean you're doing things right. But don't fret, because short-term DevOps failures can mean ...

• ### From chatbots to IBM's Watson: How software deals with conversational language

The next big thing in software development is conquering the conversational language development hurdle. Here's how the big ...

## SearchDataCenter

• ### Build a data center shutdown procedure to prepare for the worst

A data center shutdown checklist helps IT teams focus on backup, testing and system verification before pulling the plug and ...

• ### Telcos purge colocation data centers, open door to neutral connections

Enterprise customers in Verizon and CenturyLink's colocation data centers should expect better cloud and network connection ...

• ### IT slowly embraces composable infrastructure

If there will be a way to make an enterprise data center as efficient and optimized as cloud computing, composable infrastructure...

## SearchContentManagement

• ### Users prefer Office 365 suite collaboration features over SharePoint

Users don't want to jump through hoops, and they want applications to work on mobile devices. Those needs may kill traditional ...

• ### What new SharePoint features to expect in the next 12 months

Now that Microsoft is developing its SharePoint features, there are new feature rollouts all the time. Here's a rundown of the ...

• ### How WCM and AI promote smart digital user experiences

Digital user experiences and other business processes are poised to become much richer with artificial intelligence at the helm.

## SearchFinancialApplications

• ### Project planning for a new corporate performance management system

A corporate performance management system touches most aspects of any business. You need a carefully thought-out plan to ensure a...

• ### No one-size-fits-all strategy for cloud ERP software migration

Experts say a cloud ERP transition plan will vary according to a variety of factors, from company size to an organization's ...

• ### HR video apps spreading beyond training to recruiting, engagement

Users say cost savings, faster turnaround and better corporate branding are some of the benefits from using video at more stages ...

Close