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
 round on sum to calculate share

Author  Topic 

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2009-05-04 : 12:16:39
hi

my sample code:

create table temp
(person varchar(10)
,department int
,hours float)

-- drop table temp

insert into temp
select 'A',11, 11.23 union all
select 'A',12, 23.21 union all
select 'A',13, 32.10 union all
select 'B',11, 34.1 union all
select 'B',12, 20 union all
select 'B',13, 21.10 union all
select 'C',11, 24


select
person

,(select
sum(hours) from temp as t2
where t1.person = t2.person
) as total
,hours
,department
,(hours/(select
sum(hours) from temp as t2
where t1.person = t2.person
)) as share

from temp as t1



i want to get "share" round up on two decimals format "0,24" that sums per person will add = 1

i can't get it right, using round, cast and convert :)

it should be a simple solution, i just do not see it :)

thank you for your help.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-05-04 : 13:51:38
You want to round up to nearest hour?
Use CEILING.
SELECT		t.Person,
x.Hours,
t.Hours,
t.Department,
CAST(t.Hours / x.Hours AS DECIMAL(10, 2)) AS Peso,
CEILING(t.Hours / x.Hours) AS Share
FROM Temp AS t
INNER JOIN (
SELECT Person,
SUM(Hours) AS Hours
FROM Temp
GROUP BY Person
) AS x ON x.Person = t.Person



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2009-05-05 : 01:31:58
Peso thank you.

But if add additional values to this table, "Peso" column don't actually end up in sum = 1. It can also be sum = 1.01 or 0.99, etc. Column "Peso" CAST(t.Hours / x.Hours AS DECIMAL(10, 2)) should be calculated i believe first on sum and rounded (on two decimals) and than calculated on share (on two decimals).

any ideas?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-05-05 : 01:38:45
Please post some proper sample data to demonstrate your summing business rules.
Also provide exakt expected output together with a thurough explanation.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -