Request,
There are a couple of fixes we must make for your code to work:
1) When you have a SELECT that includes even one expression that is a GROUP expression [such as SUM(), MIN(), MAX(), AVG(), COUNT()], then ALL expressions in the SELECT must by GROUP expressions. IF the other expressions are not arguments of a group function (see list above), then you cause them to become group expression by listing them in a GROUP BY clause. So, your current SELECT(s), since they contain the expressions, SUM(hrs), the expression NAME must appear in a GROUP BY clause, else you receive a first-pass interpreter error.
2) Each of your SELECTs contain two expressions. Therefore, your output will be only two columns wide. To display 4-column output, each of your SELECTs must contain 4 expressions.
3) If you want report totals, you can use SQL*Plus features
To illustrate the implementation of all this, I'll show the contents of your table "TT", followed by the report script:
Table TT contents:
Select * from tt;
NAME HRS CODE
---- ---- ----
TA 1 AA
TA 1 BB
TA 1 CC
TA 1 CC
TA 1 CC
TA 1 DD
TA 1 DD
TA 1 DD
DA 4 AA
DA 4 BB
DA 1 CC
DA 1 DD
DA 1 EE
DA 1 EE
DA 1 FF
Script:
break on report
compute sum of reghrs prodhrs vachrs on report
select name, sum(hrs) reghrs,0 prodhrs, 0 vachrs
from tt
where code in ('AA','BB')
group by name
UNION
select name, 0, sum(hrs), 0
from tt
where code in ('CC','DD')
group by name
union
select name, 0, 0, sum(hrs)
from tt
where code in ('EE','FF')
group by name
order by name desc
/
NAME REGHRS PRODHRS VACHR
---- ---------- ---------- ---------
TA 0 6 0
TA 2 0 0
DA 0 0 3
DA 0 2 0
DA 8 0 0
---------- ---------- ---------
sum 10 8 3
Let me know your thoughts,
Dave