Hey,
I'm trying to calculate a percentage value in SQL. The following is my full stored procedure with sample data, if you run it you will see that I get one result which is grouped per Policy with a total for hrs_on and a total for hrs_hibernated, to calculate
my percentage I need to use the following formula
total_hrs_hibernated / total_hrs_on * 100
My problem is I can actually get the percentgae working fine if I do it for each individual record, if you run "Select * from #hibernatetemp" you will see my results with the percentage which are correct.
I however need to group my results per policy and I'm not sure how to get the calculations to work correctly when I group by policy etc.
Would appreciate any help or advice on the method to do it as I may be over thinking this and trying to do it a more difficult way,
Thanks
CREATE PROCEDURE [dbo].[SP_TEST_TEST2]
AS
/* Create temp table */
CREATE TABLE #hibernatetemp
(
on_time datetime NULL,
hibernate_time datetime NULL,
startup_after_hibernate datetime NULL,
off_time datetime NULL,
sub_policy_name varchar(50),
total_hrs_on decimal(18, 0),
total_hrs_hibernated decimal(18, 0),
hibernate_percentage decimal(18, 0)
)
/** Insert Values into Temp Table from View, including Day/Night Rates*/
insert into #hibernatetemp (on_time, hibernate_time, startup_after_hi

**View Complete Post**

## Calculating percentage of sceduled hours