[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Calculate percentage

Posted on 2009-06-28
2
Medium Priority
?
175 Views
Last Modified: 2012-05-07
I have a table with the columns Id(int), Units(int), ItemId(int), MyText(string)
I want to find all "Units" with itemId=2 and add them all together. Then I want to find how many percent the Units in each row is of the total number and return as "UnitsPercentage".  How do I do that?

Example:
Row1:  Id=1, Units=2, ItemId=2, MyText="Blue"
Row2:  Id=2, Units=4, ItemId=2, MyText="Green"
Row3:  Id=3, Units=3, ItemId=2, MyText="Yellow"
Row4:  Id=4, Units=1, ItemId=2, MyText="Red"

Return:
Id=1, UnitsPercentage="20"
Id=2, UnitsPercentage="40"
Id=3, UnitsPercentage="30"
Id=4, UnitsPercentage="10"
0
Comment
Question by:johnkainn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24731797
select distinct id, sum(units) over(partition by id) / count(*) over() as Unitspercentage
from myTable
0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 2000 total points
ID: 24731798
select distinct id, 100*sum(units) over(partition by id) / count(*) over() as Unitspercentage
from myTable
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question