|
Posted by Mladen Gogala on 03/25/06 05:34
On Fri, 24 Mar 2006 18:23:03 -0800, Greg wrote:
> I have a database that tracks the vacation time of each employee of the
> company that I work for. Everything works great and totals all of the
> vacation time that has been used. Could someone give me a little
> guidance on how I could get a subtotal for each employee and then total
> all of the subtotals? I would like to list employee 1's time and total
> it, then employee 2's time and total it and so on. Then total all of
> the subtotals. Not sure where to start to create the loop for each
> different employee.
>
> Thanks in advance for your help,
>
> Greg
Here is a select from the famous EMP & DEPT tables which does something
similar to what you want:
WITH sums AS
(SELECT deptno,
SUM(sal) AS salary
FROM emp
GROUP BY rollup(deptno))
SELECT d.deptno,
d.loc,
s.salary
FROM sums s,
dept d
WHERE s.deptno = d.deptno(+)
Results look like this:
DEPTNO LOC SALARY
---------- ------------- ----------
10 NEW YORK 8750
20 DALLAS 10875
30 CHICAGO 9400
29025
SQL>
The critical part, which provides subtotals and the final total is
"GROUP BY ROLLUP". This, however, depends on the type of the database
you are using. The statement above is from Oracle RDBMS, with version
being at least 9i. In lower versions subquery factoring (WITH ...) doesn't
work. I'm not sure about the GROUP BY ROLLUP part either. So, if your
database is not Oracle, get yourself Oracle 10XE. It is free, you can
stuff 4GB in it and you can do things like the query above with it.
It supports subquery factoring, group by rollup and much, much more.
--
http://www.mgogala.com
[Back to original message]
|