Solved

Calculate percentage

Posted on 2009-06-28
2
166 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 500 total points
ID: 24731798
select distinct id, 100*sum(units) over(partition by id) / count(*) over() as Unitspercentage
from myTable
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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