we have created following anonymous block………..
DECLARE
sql_str long(32000);
where_str long(32000);
counter NUMBER(3):=0;
BEGIN
sql_str:='SELECT '||' A.bio_id ,';
where_str:=' where '||'A.bio_id=B.bio_id AND'||' A.bio_id<>0 and rownum<25 AND (' ;
LOOP
counter:=counter+1;
sql_str:=sql_str||'decode(A.wk_units'||(counter+1)||' - B.wk_units'|| (counter)||',0,NULL,A.wk_units'||(counter+1)||')';
sql_str:=sql_str||', decode(A.wk_units'||(counter+1)||' - B.wk_units'|| (counter)||',0,NULL,B.wk_units'||(counter)||')' ;
where_str:=where_str||' A.wk_units'||(counter+1)||'<> B.wk_units'||(counter) ;
EXIT WHEN counter=5;
sql_str:=sql_str||', ';
where_str:=where_str||' or ';
END LOOP;
sql_str:=sql_str||' from cnt_sls_dm.fct_sales_summary A, cnt_sls_dm.fct_sales_summary B';
sql_str:=sql_str||where_str||')';
dbms_output.put_line(sql_str);
EXECUTE IMMEDIATE(sql_str);
END;
Result needed:
We have written a dynamic sql query.
It should get the result set of select query.
But we are getting the query itself on running this block.
rather than getting any result for this query.
Let us know are we on the rite track.
Or we need to do something else to get the result out.
the result is
SQL> /
SELECT A.bio_id ,decode(A.wk_units2 - B.wk_units1,0,NULL,A.wk_units2),
decode(A.wk_units2 - B.wk_units1,0,NULL,B.wk_units1), decode(A.wk_units3 -
B.wk_units2,0,NULL,A.wk_units3), decode(A.wk_units3 -
B.wk_units2,0,NULL,B.wk_units2), decode(A.wk_units4 -
B.wk_units3,0,NULL,A.wk_units4), decode(A.wk_units4 -
B.wk_units3,0,NULL,B.wk_units3), decode(A.wk_units5 -
B.wk_units4,0,NULL,A.wk_units5), decode(A.wk_units5 -
B.wk_units4,0,NULL,B.wk_units4), decode(A.wk_units6 -
B.wk_units5,0,NULL,A.wk_units6), decode(A.wk_units6 -
B.wk_units5,0,NULL,B.wk_units5) from cnt_sls_dm.fct_sales_summary A,
cnt_sls_dm.fct_sales_summary B where A.bio_id=B.bio_id AND A.bio_id<>0 and
rownum<25 AND ( A.wk_units2<> B.wk_units1 or A.wk_units3<> B.wk_units2 or
A.wk_units4<> B.wk_units3 or A.wk_units5<> B.wk_units4 or A.wk_units6<>
B.wk_units5)
PL/SQL procedure successfully completed.
For me it is unclear what Irveen wants to achieve:
@Irveen, Why not a table with 105 records? Every week you drop one record and you add enother one. You will have never to change your query.
Another solution is to shift your data, so you put the data of column 2 into 1, 3 into 2, 4 into 3, …, 103 into 102. You will never have to add a column and drop a column.