adding sub total to the query

referring to this question

i have this query

select CATEGORYID,cat , gross, net, discount, 
(to_char(nvl(100 * (gross/sum(gross) over()),0),'fm9990.00')||'%' ) Cat_Perc,
(to_char(nvl(100 * (Discount/gross),0),'fm9990.00')||'%') Disc_Perc,
 sum(gross) over() total_price from (
select CATEGORYID, cat ,
sum(PRICE) Gross,sum(DISCPRIC) Net,(sum(discount)) Discount , sum(quantity) QTY 
TO_char(dob, 'dd-mm-yyyy') >= '01-04-2015'
and TO_char(dob, 'dd-mm-yyyy') <= '30-04-2015'
group by CATEGORYID, cat )
order by gross desc ;

Open in new window

this query deplays catigory and sales amounts
ok , if i want to add a row for a specific selection of catigory
for example , catigory in (2,3,4)
add sum(gross) , sum(net), sum(disount)
to the next of catigory number 4
then continue other records
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

awking00Information Technology SpecialistCommented:
Can you provide some sample data for sales_det and what you would like to see as results?
slightwv (䄆 Netminder) Commented:
I do not understand what you are asking that is different.

If you want a SUM on every row for various categories, just do the SUM with CASE and OVER from the previous question.

If you want something different, please post sample data and expected results so we can see what you are trying to do.
NiceMan331Author Commented:
If you want a SUM on every row for various categories
just do the SUM with CASE and OVER from the previous question.
you mean it will be added column ? ok , is it possible to do it as rows under various catigories ?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

slightwv (䄆 Netminder) Commented:
>>you mean it will be added column


>> is it possible to do it as rows under various catigories ?

This is different than what you stated "exactly" in above when I asked "If you want a SUM on every row for various categories"

If you want a new row to contain the sub totals, it might be able to be done but not easily.  Remember in a previous question of yours I mentioned UNION ALL and some smoke and mirrors?

This is what I meant.

It will need to be a separate query that returns the sub-totals.  You'll need to include some sort columns in ALL the queries so when you do an "order by" the rows come back in the correct order.
NiceMan331Author Commented:
can you post an example of that considering when catigoryid < 4 only
then i can adjust the others
slightwv (䄆 Netminder) Commented:
sum(case when gross < 4 then gross end) over()
NiceMan331Author Commented:
This is as a column , I need an example as your suggestion to make row of sub total
slightwv (䄆 Netminder) Commented:
Here is a simple test case showing what I mean:
drop table tab1 purge;
create table tab1(group_name char(1), qty number);

insert into tab1 values('a',1);
insert into tab1 values('a',1);
insert into tab1 values('a',1);
insert into tab1 values('b',2);
insert into tab1 values('b',2);

select case when sort_order = 2 then 'Sub Total' else group_name end my_label, qty from (
	select 1 sort_order, group_name, qty from tab1
	union all
	select 2 sort_order, group_name, sum(qty) from tab1 group by group_name
order by group_name, sort_order

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NiceMan331Author Commented:
Ok I got it , let me do my calculation on Saturday , cause I'm now day off , thanx
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.