Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating Percentage for each line

Status
Not open for further replies.

bmacbmac

IS-IT--Management
Joined
Jan 26, 2006
Messages
392
Location
US
Hi,

In my table I have three colums with the following data:

Market Amount AmountPercent
CT 705000 NULL
FAHT 1100000 NULL
LT 441000 NULL
STG 221100 NULL

I am trying to set the 'AmountPercent' column to the percentage of total amount sum.

I'm trying this:
Code:
select amount/sum(amount)from #temp
group by market, amount

But it just gives my percentage as 1.0000 for each line. I'm probably grouping wrong, but everything I try gives me a 1.0000 percentage.

Any ideas?

Thanks!
 
Sorry, my code says I am doing a 'select' statement, but I actually need to do an update.

Also, this is SQL 2000.

Thanks for looking!
 
Your grouping is wrong. And you may run in to integer math issues (if your amount column is an integer)

I would suggest you do this in parts. Like this...

Code:
Declare @Total Int
Select @Total = Sum(Amount) From #Temp

Update #Temp
Set    AmountPercent = 100.0 * Amount / @Total

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks gmmastros.... worked like a champ!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top