Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Percentage

Author  Topic 

mukhan85
Starting Member

46 Posts

Posted - 2008-09-05 : 16:44:51
Hi, I have the following table:
Quantity MONTH
Apple 49 1
Banana 120 1
Orange 55 1

The total Quantity is 224 now I need the following result:
Percentage MONTH
Apple 21.8% 1
Banana 53.6% 1
Orange 24.5% 1

I appreciate your help. Thank you.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-09-05 : 16:54:52
SELECT Column1, Quantity*100.0/(SELECT SUM(Quantity) AS TotalQuantity FROM YourTable), MONTH
FROM YourTable

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-06 : 23:38:49
small modification (just in case your table has more than one months data
SELECT t.Column1, 
t.Quantity*100.0/
(SELECT SUM(Quantity) AS TotalQuantity
FROM YourTable
WHERE MONTH=t.MONTH),
t.MONTH
FROM YourTable t
Go to Top of Page
   

- Advertisement -