## AdBlock Detected!

Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.

## SQL Percent To Total |

SQL > Advanced SQL >
Percent To Total
To calculate percent to total in SQL, we need to first calculate the total, and then we divide each individual value by the total to find the percentage. So this is a two-step process. There are multiple ways to accomplish this. Here we will show three different ways: ## Inline View in SELECTThe first method is to use the inline view construct in the Let's use an example to illustrate. Say we have the following table, Table
we would type, SELECT a1.Name, a1.Sales, a1.Sales * 1.0/(SELECT SUM(Sales) FROM Total_Sales) Pct_To_Total
FROM Total_Sales a1 ORDER BY a1.Sales DESC, a1.Name DESC; Result:
The inline view We include ## Inline View in FROMThe second method is to the inline view in the SELECT a1.Name, a1.Sales, a1.Sales * 1.0 / a2.Total Pct_To_Total
FROM Total_Sales a1, (SELECT SUM(Sales) Total FROM Total_Sales) a2 ORDER BY a1.Sales DESC, a1.Name DESC; ## Using Common Table Expression (CTE)A third way to calculate percent to total is to use the Common Table Expression (CTE). In this case, we will first use the WITH Total_Sum AS (
SELECT SUM(Sales) Total FROM Total_Sales ) SELECT a1.Name, a1.Sales, a1.Sales * 1.0 / a2.Total Pct_To_Total FROM Total_Sales a1, Total_Sum a2 ORDER BY a1.Sales DESC, a1.Name DESC; ## List of SQL Complex Operations
SQL Cumulative Percent To Total |

Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.