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
 Add up fields & divide in Sub-select query in SQL?

Author  Topic 

Maverick_
Posting Yak Master

107 Posts

Posted - 2010-11-11 : 11:08:21
Hi all I am trying to do a round up inside a sub-select but can't think of a way to do it.

This is my initial Sub-select:

(SELECT sum(f_meas.feature_quantity)
FROM
feat_measurement f_meas
WHERE
f_meas.measurement_code IN ('TVAL', 'TSDA', 'TSCO, 'TMOV')) as Roundup

This is an Excel equivalent example of what I am trying to achieve with the above Sub-select:

sum(roundup(sum('TVAL', 'TSDA', 'TSCO, 'TMOV')/4,0)).

Is there a way to do this in SQL?

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-11-11 : 11:32:14
I'm not familar with the roundup function, but maybe the ROUND function?
Go to Top of Page

Maverick_
Posting Yak Master

107 Posts

Posted - 2010-11-11 : 11:38:12
Yes, but what I am looking for it to do in SQL is Add up quantity it finds in TVAL, TSDA, TSCO, and TMOV fields, then divide it by 4 and give the total.

Is there a way to do that in SQL?
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-11-11 : 11:53:30
Maybe:
(SELECT ROUND(sum(f_meas.feature_quantity / 4.0), 0)
FROM
feat_measurement f_meas
WHERE
f_meas.measurement_code IN ('TVAL', 'TSDA', 'TSCO', 'TMOV')) AS RoundUp

If that doesn't work, please provide DDL, DML and expected output. This link can help you with that:
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

Maverick_
Posting Yak Master

107 Posts

Posted - 2010-11-11 : 12:08:48
Lamprey that did it! Thank you :)
Go to Top of Page
   

- Advertisement -