Store COMPUTE in variable?

narmi2
narmi2 used Ask the Experts™
on
Dear Experts,

Is it possible to store the COMPUTE value in a variable for later usage?

SELECT employee_name, hours_done
FROM work_done
GROUP BY employee_name
COMPUTE SUM( hours_done )
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
No, it's a reporting feature.
Besides, it has been deprecated in favour of ROLLUP.
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
ROLLUP version

declare @T table (employee_name varchar(100), hours_done int)
insert @t
SELECT employee_name, SUM(hours_done)
FROM work_done
GROUP BY rollup(employee_name)

declare @V int
select @V=hours_done from @t where employee_name is null -- rollup sum
print @V

==========

of course for the simple query you had, it is equivalent to

declare @V int
select @V=SUM(hours_done) FROM work_done -- compute sum over all employees = all data
print @V

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial