473,396 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Calculating percentages

Hi All,
I have an Oracle database I am trying to run report queries against.
I would like to calculate the ratio of a value to the sum of values in the data partition.
The question I have is whether to use the RATIO_TO_REPORT function or the OVER and PARTITION BY functions.
I have tried writing queries both ways and seem to get hung up on the COUNT and calculating percentages.
Any insight is greatly appreciated.
PaulT

--Example Queries
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT ProductGroup, Product, ProductCount, (ProductCount/TotalCount) AS 'Percentage'
  2.  From (
  3.     Select ProductGroup, Product,
  4.            Sum(subtotal) Over () As 'TotalCount',
  5.            Sum(subtotal) Over (Partition By Product) As 'ProductCount'
  6.     From Table1
  7.     Where Product in ('Product A', 'Product B', 'Product C'))
  8.  Where Product in ('Product A', 'Product B', 'Product C')
  9.  Order by 1 DESC, 2;
--Alternate Example Query
Expand|Select|Wrap|Line Numbers
  1. SELECT ProductGroup, Product, COUNT(Product),
  2. RATIO_TO_REPORT (SUM(Product))
  3. OVER (PARTITION BY Product) Percentage
  4. from Table1
  5. Where Product in ('ProductA', 'ProductB', 'ProductC')
  6. Group by ProductGroup, Product;

--Example Database table:
Expand|Select|Wrap|Line Numbers
  1. ProductGroup        Product        SerialNum
  2. Product Group 1        Product A    S/N 12345
  3. Product Group 1        Product A    S/N 56789
  4. Product Group 1        Product B    S/N 34567
  5. Product Group 1        Product B    S/N 67890
  6. Product Group 1        Product B    S/N 98765
  7. Product Group 1        Product C    S/N 12312
  8. Product Group 1        Product C    S/N 56756
  9. Product Group 2        Product E    S/N 99999
  10. Product Group 2        Product E    S/N 88888
  11. Product Group 2        Product F    S/N 44444
  12. Product Group 2        Product F    S/N 33333
What I would like to end up with is something which looks like this showing the count and percentage which that count represents of the subgroup:
Expand|Select|Wrap|Line Numbers
  1. ProductGroup        Product        Count    Percentage
  2. Product Group 1        Product A    2    .285
  3. Product Group 1        Product B    3    .428
  4. Product Group 1        Product C    2    .285
  5. Product Group 2        Product E    2    .5
  6. Product Group 2        Product F    2    .5
Feb 16 '12 #1

✓ answered by Rabbit

You've over-complicated things. What you want is something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT groupField, 
  2.    COUNT(*) AS SubCount, 
  3.    COUNT(*) / (SELECT COUNT(*) FROM someTable) AS Percentage
  4. FROM someTable
  5. GROUP BY groupField

2 11481
Rabbit
12,516 Expert Mod 8TB
You've over-complicated things. What you want is something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT groupField, 
  2.    COUNT(*) AS SubCount, 
  3.    COUNT(*) / (SELECT COUNT(*) FROM someTable) AS Percentage
  4. FROM someTable
  5. GROUP BY groupField
Feb 16 '12 #2
Thanks Rabbit, it worked like a charm!
Feb 17 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: michael | last post by:
I've got a 3-column layout using percentages to define width of each div-column. #left-col { width: 20%; background-color: green; float:left; } #center-col { width: 60%; background-color: blue;...
1
by: Megan | last post by:
quick summary: i'm having problems trying to group fields in a report in order to calculate percentages. to calculate percentages, i'm comparing the results from my grouped fields to the totals....
3
by: JDiamond | last post by:
Hi, I have a table called Hosts. The Hosts table contains the following fields: Each field represents a step in the project. The tech that completes each step initials the respective...
26
by: Mike Barnard | last post by:
Hi all. I am playing with html and css. I don't (yet) have a working site, I'm just trying to build a working, basic template I can use for a couple of ideas I have. I recall reading a...
5
by: Chris H | last post by:
I am wanting to write a function for a poker league manager that will detrmine players finishing points in a tournament depending on the amount of players that played... In other words when the...
5
by: jonm4102 | last post by:
I'm trying to calculate the median of some numerical data. The data can only be found in a query (henceforth query 1) field I previously made, and I would prefer to calculate the median in a new...
1
by: Bob Alston | last post by:
I need to produce a report like this Color: # % ------ --- --- White 10 20 Black 25 50 other 15 30 ---- ---- Total 50 100
9
by: AZKing | last post by:
Hi all, I would like to know how do you go about calculating percentages in Access. For example, in a form I have 3 combo boxes with drop down menus where a user can select "Yes" or "No" and a...
5
by: eliana82 | last post by:
I have problems calculating score percentages within groups. I have created a boat program in access where the information provided is name, team, boat and score. The first query I've done is...
1
by: Dineo | last post by:
Hi there I've created a database and a pivot table but I need to calculates percentages of all the WithdrwalAmt (the amount being withdrawn when retire) per PayeeName (where the money was invested...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.