A reader, December 16, 2002 - 9:03 am UTC
THANK YOU VERY MUCH TOM
A reader, December 18, 2002 - 8:30 am UTC
Tom,
In the query you showed me you selected call act. and call scheduled from only one region (where region.name like ''%'' || :p_regionname || ''%'')...What if I want to select call act. and call sched. where region.level1=9 along with call act. and call sched where region.level1=10
along with call act. and call sched where region.level1=125.
How should I proceed? Separate query with union since to all of them we have to apply different where clause in regard to region level or else?
Thank You very much
December 18, 2002 - 12:41 pm UTC
Is it that you want 9 rows back (3 for each "region") or 3 rows back across all regions.
A reader, December 20, 2002 - 9:07 am UTC
Tom,
Thanks very much for you quick response. I did how you suggested and everythin is working perfect...
Following is another question pertaining to the same quiry:
select sum(case
when (question.questiontext='Enter Display Type' and answer.answerinteger=1
)
then 1
else 0
end) "System XL",
sum(case
when (question.questiontext='Enter Display Type' and answer.answerinteger=2
)
then 1
else 0
end) "Vista",
sum(case
when (question.questiontext='Enter Display Type' and answer.answerinteger=3
)
then 1
else 0
end) "Other",
sum(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours'
)
then answer.answerinteger
else 0
end) "(Included) SPP Hrs.",
avg(case
when (question.questiontext='Enter Display Width in Feet'
)
then answer.answerinteger
else 0
end) "(Included) SPP Hrs."
FROM outlet,
call,
question,
answer,
region,
mapregionoutlet,
chain,
project
WHERE outlet.outletid=call.outletid
and call.callid=answer.callid
and answer.questionid=question.questionid
and outlet.outletid=mapregionoutlet.outletid
and mapregionoutlet.regionid=region.regionid
and outlet.chainid=chain.chainid
and call.projectid=project.projectid
and call.duedate between to_date('1/1/2001','mm/dd/yyyy') and to_date('12/31/2001','mm/dd/yyyy')
and chain.chainid=132
and project.name in ('Secondary Maybelline Reset','Maybelline Reset')
and region.level1=9
For the first 3 cases I applied condition of project.name in ('Secondary Maybelline Reset','Maybelline Reset')
However, for the last case I don't want to include anything where project.name in 'Maybelline Reset', only where project name in 'Secondary Maybelline Reset'.
I've tried to put that filter inside of each case, however it runs forever, so I had to cancel the operation.
Thanks again
December 20, 2002 - 9:21 am UTC
avg(case
when (question.questiontext='Enter Display Width in Feet' and
project.name = 'Secondary Maybelline Reset'
)
then answer.answerinteger
else NULL
end) "(Included) SPP Hrs."
add the predicate in the case, return NULL, not 0, and it won't be averaged in.
A reader, December 20, 2002 - 11:01 am UTC
Thanks a lot, everything works.
How can I make following select statement work for many different regions, which is the first inner select statement I need to repeat 5 times just to change region.level1 to region.level2=10,then region.level2=120 and so on. If I just concatenate them all the select statement will be humangous. Would it be better and possible for me to write function to return let's say 10 columns based on the region.level
December 20, 2002 - 11:14 am UTC
why not
and region.level1 in ( .... )
group by region.level1
and add region.level1 to the select list. get a row/region?
A reader, December 20, 2002 - 11:02 am UTC
I forgot to include select statement:
select decode(rn,1,c1,2,c2,3,c3,4,c4,5,c5,6,c6,7,c7,c8) "NATIONAL"
from
(select
sum(case
when (question.questiontext='Enter Display Width in Feet' and project.name = 'Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end) c1,
sum(case
when (question.questiontext='Enter Display Type' and answer.answerinteger=1 and project.name = 'Maybelline Reset' and region.level1=9)
then 1
else 0
end) c2,
sum(case
when (question.questiontext='Enter Display Type' and answer.answerinteger=2 and project.name = 'Maybelline Reset' and region.level1=9)
then 1
else 0
end) c3,
sum(case
when (question.questiontext='Enter Display Type' and answer.answerinteger=3 and project.name = 'Maybelline Reset' and region.level1=9)
then 1
else 0
end) c4,
sum(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours' and project.name = 'Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end) c5,
sum(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours' and project.name = 'Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end)+
sum(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours' and project.name = 'Secondary Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end) c6,
to_char(to_number(avg(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours'and project.name in ('Maybelline Reset') and region.level1=9)
then answer.answerinteger
else 0
end)),'9.99')c7,
to_char(to_number(avg(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours'and project.name in ('Maybelline Reset','Secondary Maybelline Reset') and region.level1=9)
then answer.answerinteger
else 0
end)),'9.99') c8
FROM outlet,
call,
question,
answer,
region,
mapregionoutlet,
chain,
project
WHERE outlet.outletid=call.outletid
and call.callid=answer.callid
and answer.questionid=question.questionid
and outlet.outletid=mapregionoutlet.outletid
and mapregionoutlet.regionid=region.regionid
and outlet.chainid=chain.chainid
and call.projectid=project.projectid
and call.duedate between to_date('1/1/2001','mm/dd/yyyy') and to_date('12/31/2001','mm/dd/yyyy')
and chain.chainid=132
and project.name in ('Secondary Maybelline Reset','Maybelline Reset')),
( select 1 rn from dual
union all
select 2 rn from dual
union all
select 3 rn from dual
union all
select 4 rn from dual
union all
select 5 rn from dual
union all
select 6 rn from dual
union all
select 7 rn from dual
union all
select 8 rn from dual
)
--and region.level1=9
A reader, December 20, 2002 - 11:44 am UTC
I am sorry Tom but I did not get it...
How the function would look like with following select statement and return something like that:
Heading -->c1 c2 c3 c4
Value -->12 23 32 423
select
sum(case
when (question.questiontext='Enter Display Width in Feet' and project.name = 'Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end) c1,
sum(case
when (question.questiontext='Enter Display Type' and answer.answerinteger=1 and project.name = 'Maybelline Reset' and region.level1=9)
then 1
else 0
end) c2,
sum(case
when (question.questiontext='Enter Display Type' and answer.answerinteger=2 and project.name = 'Maybelline Reset' and region.level1=9)
then 1
else 0
end) c3,
sum(case
when (question.questiontext='Enter Display Type' and answer.answerinteger=3 and project.name = 'Maybelline Reset' and region.level1=9)
then 1
else 0
end) c4,
sum(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours' and project.name = 'Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end) c5,
sum(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours' and project.name = 'Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end)+
sum(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours' and project.name = 'Secondary Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end) c6,
to_char(to_number(avg(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours'and project.name in ('Maybelline Reset') and region.level1=9)
then answer.answerinteger
else 0
end)),'9.99')c7,
to_char(to_number(avg(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours'and project.name in ('Maybelline Reset','Secondary Maybelline Reset') and region.level1=9)
then answer.answerinteger
else 0
end)),'9.99') c8
FROM outlet,
call,
question,
answer,
region,
mapregionoutlet,
chain,
project
WHERE outlet.outletid=call.outletid
and call.callid=answer.callid
and answer.questionid=question.questionid
and outlet.outletid=mapregionoutlet.outletid
and mapregionoutlet.regionid=region.regionid
and outlet.chainid=chain.chainid
and call.projectid=project.projectid
and call.duedate between to_date('1/1/2001','mm/dd/yyyy') and to_date('12/31/2001','mm/dd/yyyy')
and chain.chainid=132
and project.name in ('Secondary Maybelline Reset','Maybelline Reset')
December 20, 2002 - 11:55 am UTC
select region.level1,
sum(case
when (question.questiontext='Enter Display Width in Feet' and
project.name = 'Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end) c1,
sum(case
when (question.questiontext='Enter Display Type' and
answer.answerinteger=1 and project.name = 'Maybelline Reset' and
region.level1=9)
then 1
else 0
end) c2,
sum(case
when (question.questiontext='Enter Display Type' and
answer.answerinteger=2 and project.name = 'Maybelline Reset' and
region.level1=9)
then 1
else 0
end) c3,
sum(case
when (question.questiontext='Enter Display Type' and
answer.answerinteger=3 and project.name = 'Maybelline Reset' and
region.level1=9)
then 1
else 0
end) c4,
sum(case
when (question.questiontext='Enter Total Time Spent in the Store, in
Hours' and project.name = 'Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end) c5,
sum(case
when (question.questiontext='Enter Total Time Spent in the Store, in
Hours' and project.name = 'Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end)+
sum(case
when (question.questiontext='Enter Total Time Spent in the Store, in
Hours' and project.name = 'Secondary Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end) c6,
to_char(to_number(avg(case
when (question.questiontext='Enter Total Time Spent in the Store, in
Hours'and project.name in ('Maybelline Reset') and region.level1=9)
then answer.answerinteger
else 0
end)),'9.99')c7,
to_char(to_number(avg(case
when (question.questiontext='Enter Total Time Spent in the Store, in
Hours'and project.name in ('Maybelline Reset','Secondary Maybelline Reset') and
region.level1=9)
then answer.answerinteger
else 0
end)),'9.99') c8
FROM outlet,
call,
question,
answer,
region,
mapregionoutlet,
chain,
project
WHERE outlet.outletid=call.outletid
and call.callid=answer.callid
and answer.questionid=question.questionid
and outlet.outletid=mapregionoutlet.outletid
and mapregionoutlet.regionid=region.regionid
and outlet.chainid=chain.chainid
and call.projectid=project.projectid
and call.duedate between to_date('1/1/2001','mm/dd/yyyy') and
to_date('12/31/2001','mm/dd/yyyy')
and chain.chainid=132
and project.name in ('Secondary Maybelline Reset','Maybelline Reset')
and region.level1 in ( 9, 10, 11, ... )
group by region.level1
you would get a row/region.
Are you some kind of machine ?
C, December 20, 2002 - 1:49 pm UTC
Tom, I really don't know... Are you really human ???
Sometimes I just don't believe you ALONE (I know other experts step in some rare times) can answer such long, complex and -- many times -- dull and boring questions.
How do you make it ?
It just doesn't make sense... In addition to spending soooo muuuuuch time on this site (working Saturdays and Sundays too), you must keep up-to-date with the Oracle DB, you are a VP for government, education and health, you write lengthy and awesome books, gives lectures, etc, etc, etc.
IT JUST DOESN'T MAKE SENSE...
Do you get any leisure at all ? What do you do with your spare time ?
Either you don't exist or you are some kind of godlike creature.
Hasn't Oracle just made you up ?
A reader, December 20, 2002 - 2:42 pm UTC
Sorry if I did not specify it more clear, but the first row should be based on pridicate region.level1=9 and going forward the predicate should be based on region.level2 in (10,120,245).How to do it? With the following select statement I got 3 requeired with level 2, but did not get first one with level 1
select
sum(case
when (question.questiontext='Enter Display Width in Feet' and project.name = 'Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end) c1,
sum(case
when (question.questiontext='Enter Display Type' and answer.answerinteger=1 and project.name = 'Maybelline Reset' and region.level1=9)
then 1
else 0
end) c2,
sum(case
when (question.questiontext='Enter Display Type' and answer.answerinteger=2 and project.name = 'Maybelline Reset' and region.level1=9)
then 1
else 0
end) c3,
sum(case
when (question.questiontext='Enter Display Type' and answer.answerinteger=3 and project.name = 'Maybelline Reset' and region.level1=9)
then 1
else 0
end) c4,
sum(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours' and project.name = 'Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end) c5,
sum(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours' and project.name = 'Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end)+
sum(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours' and project.name = 'Secondary Maybelline Reset' and region.level1=9)
then answer.answerinteger
else NULL
end) c6,
to_char(to_number(avg(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours'and project.name in ('Maybelline Reset') and region.level1=9)
then answer.answerinteger
else 0
end)),'9.99')c7,
to_char(to_number(avg(case
when (question.questiontext='Enter Total Time Spent in the Store, in Hours'and project.name in ('Maybelline Reset','Secondary Maybelline Reset') and region.level1=9)
then answer.answerinteger
else 0
end)),'9.99') c8
FROM outlet,
call,
question,
answer,
region,
mapregionoutlet,
chain,
project
WHERE outlet.outletid=call.outletid
and call.callid=answer.callid
and answer.questionid=question.questionid
and outlet.outletid=mapregionoutlet.outletid
and mapregionoutlet.regionid=region.regionid
and outlet.chainid=chain.chainid
and call.projectid=project.projectid
and call.duedate between to_date('1/1/2001','mm/dd/yyyy') and to_date('12/31/2001','mm/dd/yyyy')
and chain.chainid=132
and project.name in ('Secondary Maybelline Reset','Maybelline Reset')
and region.level1 in (0,9)
and region.level2 in ( 0,9, 10, 120,245 )
group by region.level2,region.level1
December 20, 2002 - 2:59 pm UTC
if I understand, I think this is it:
Select
sum(case
when (question.questiontext='Enter Display Width in Feet' and project.name = 'Maybelline Reset' and region.level1 in ( 9, 10 ) )
then answer.answerinteger
else NULL
end) c1_lvl1,
sum(case
when (question.questiontext='Enter Display Width in Feet' and project.name = 'Maybelline Reset' and region.level2 in ( 10, 120, 245 ) )
then answer.answerinteger
else NULL
end) c1_lvl2,
.....
FROM outlet,
call,
question,
answer,
region,
mapregionoutlet,
chain,
project
WHERE outlet.outletid=call.outletid
and call.callid=answer.callid
and answer.questionid=question.questionid
and outlet.outletid=mapregionoutlet.outletid
and mapregionoutlet.regionid=region.regionid
and outlet.chainid=chain.chainid
and call.projectid=project.projectid
and call.duedate between to_date('1/1/2001','mm/dd/yyyy') and
to_date('12/31/2001','mm/dd/yyyy')
and chain.chainid=132
and project.name in ('Secondary Maybelline Reset','Maybelline Reset')
and ( region.level1 in (0,9) or region.level2 in ( 0,9, 10, 120,245 ) )
I think you need an "or" and then use the in again in the case statement.
that'll give you a single row with all of the numbers.
A reader, December 20, 2002 - 3:57 pm UTC
Does not work...
I need a function which would return only one record...
However, predicate in that function should vary. On one occasion I need to put in predicate 'where region.level1=9'
on another occasion I need to put in predicate 'where region.level2=10'.How can I accomplish that?
December 20, 2002 - 8:11 pm UTC
native dynamic sql.
..
is
type rc is ref cursor;
l_cursor rc;
..
begin
open l_cursor for your_query || your_predicate using your_bind variables...
A reader, December 23, 2002 - 9:28 am UTC
Tom,
What am I doing wrong? The following function does not work...And would it be possibile to pass 'v_where' value as a parameter as well?
FUNCTION F_ACC_RESET_ACC_DTL_COSM(p_region_level INTEGER)
RETURN PKG_RPT_ACC_RESET_ACC_DTL_COSM.v_refcursor
IS
v_where VARCHAR2(100) ;
l_cursor PKG_RPT_ACC_RESET_ACC_DTL_COSM.v_refcursor;
BEGIN
v_where:='and region.level1';
open l_cursor for 'select
sum(case
when (question.questiontext=''Enter Display Width in Feet'' and project.name = ''Maybelline Reset'' )
then answer.answerinteger
else NULL
end) c1,
sum(case
when (question.questiontext=''Enter Display Type'' and answer.answerinteger=1 and project.name = ''Maybelline Reset'' )
then 1
else 0
end) c2,
sum(case
when (question.questiontext=''Enter Display Type'' and answer.answerinteger=2 and project.name = ''Maybelline Reset'')
then 1
else 0
end) c3,
sum(case
when (question.questiontext=''Enter Display Type'' and answer.answerinteger=3 and project.name = ''Maybelline Reset'')
then 1
else 0
end) c4,
sum(case
when (question.questiontext=''Enter Total Time Spent in the Store, in Hours'' and project.name = ''Maybelline Reset'')
then answer.answerinteger
else NULL
end) c5,
sum(case
when (question.questiontext=''Enter Total Time Spent in the Store, in Hours'' and project.name = ''Maybelline Reset'')
then answer.answerinteger
else NULL
end)+
sum(case
when (question.questiontext=''Enter Total Time Spent in the Store, in Hours'' and project.name = ''Secondary Maybelline Reset'')
then answer.answerinteger
else NULL
end) c6,
to_char(to_number(avg(case
when (question.questiontext=''Enter Total Time Spent in the Store, in Hours''and project.name in (''Maybelline Reset'') )
then answer.answerinteger
else 0
end)),''9.99'')c7,
to_char(to_number(avg(case
when (question.questiontext=''Enter Total Time Spent in the Store, in Hours''and project.name in (''Maybelline Reset'',''Secondary Maybelline Reset'') )
then answer.answerinteger
else 0
end)),''9.99'') c8
FROM outlet,
call,
question,
answer,
region,
mapregionoutlet,
chain,
project
WHERE outlet.outletid=call.outletid
and call.callid=answer.callid
and answer.questionid=question.questionid
and outlet.outletid=mapregionoutlet.outletid
and mapregionoutlet.regionid=region.regionid
and outlet.chainid=chain.chainid
and call.projectid=project.projectid
and call.duedate between to_date(''1/1/2001'',''mm/dd/yyyy'') and to_date(''12/31/2001'',''mm/dd/yyyy'')
and chain.chainid=132
and project.name in (''Secondary Maybelline Reset'',''Maybelline Reset'') ' || v_where =':p_region_level'
--and region.level1=:p_region_level'
using p_region_level;
RETURN l_cursor ;
END;
December 23, 2002 - 11:14 am UTC
v_where:='and region.level1';
....
and project.name in (''Secondary Maybelline Reset'',''Maybelline Reset'') '
|| v_where =':p_region_level'
^^^^ should be || v_where || ' = :p_region_level'
using p_region_level;
RETURN l_cursor ;