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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Calculating Percentages

Author  Topic 

phoenix22
Starting Member

20 Posts

Posted - 2006-01-19 : 17:29:35
Does anyone have any suggestions as to how I can easily calculate percentages in one compact query? (eg. Overtime hours divided by regular hours where the hour type is indicated by a flag)

Thanks in advance

SamC
White Water Yakist

3467 Posts

Posted - 2006-01-19 : 18:05:51
SELECT SUM(Overtime) / SUM(Regular) * 100.0 as Percent, Fullname FROM MyTable GROUP BY Fullname
Go to Top of Page

phoenix22
Starting Member

20 Posts

Posted - 2006-01-19 : 22:39:15
Thanks for your reply. The problem is that the table doesn't have a separate column for hours and overtime hours. The table structure is as follows:

Hours Type
34 Overtime
22 Reg
10 Sick
11 Reg
2 Overtime

Is there a way to calculate the percentage of reg hours over OT hours just by using one query? Thanks
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-01-19 : 22:50:36
Something like this ? If not, post your table structure (not partial)
select 	[percent] = (select sum(hours) from MyTable where type = 'Overtime')  * 100.0 / 
(select sum(hours) from MyTable where type = 'Reg')



-----------------
'KH'

Go to Top of Page
   

- Advertisement -