Could someone help to get me started here.
Need to make a 'crosstab' transformation (the rows become columns).
One can use Decode(...) in a 'static' case but what about when the number of coulmns can't be defined in advance?
If someone by chance has example, it'll be greatly appreciated.
To my mind the solution is trivial - if you want a layout like Excel, extract your data as a "flat file" and load it into Excel. (Been doing this for years . . . a few lines of macro will automate it as well.)
OK, analytical functions will do some of this stuff - but why not do it the easy way?
"The power of instruction is seldom of much efficacy except in those happy dispositions where it is almost superfluous" - Gibbon, quoted by R.P.Feynman
Thanks for your reply.
It could have been a solution but not in the situation I'm facing.
All the data remains on Oracle, no output to Excel is desirable.
I'm more than sure that it's doable by using dynamic sql and 'decode'
function.
I'd use CURSOR with ORDER BY in PL/SQL, loop over the rows and build up the output string by concatination (with break-point processing just as we did in the 70's).
If you really want the ouput in an Oracle table with a variable number of columns . . . .
"The power of instruction is seldom of much efficacy except in those happy dispositions where it is almost superfluous" - Gibbon, quoted by R.P.Feynman
should have been ammended (',' added in the very beginning:
So it worked fine.
Here comes my second question.I tried to 'complicate' the matter:
1: CREATE OR REPLACE PACKAGE BODY ChartNameHistoryBySource
2: AS
3: PROCEDURE ChartNameHistoryBySource ,
4: (
5: p_StoreDateStart IN DATE
6: p_StoreDateEnd IN DATE
7: p_CompanyName IN VarChar2
8: p_Maturity IN Integer
9: ChartOutput IN OUT ReturnCursor
10: )
11:
12: IS
13: l_query long := 'SELECT storedate ';
14:
15: BEGIN
16: FOR x IN (SELECT DISTINCT source FROM tbltestsources)
17:
18:
19:
33:
34:
35:
36: LOOP
37: l_query:= l_query||
38: ','||'max (DECODE (source,’’’|| x.source ||’’’, MIDPX ,null)) "'|| x. source ||' " ’;
39: END LOOP;
40:
41:
42:
43:
44: l_query :=rtrim(l_query, ',') ||
45: ' FROM tblcdsbysource cds
46: , Itblcds cd
47: , tblnames n
48: , tblsources srs
49:
I50: WHERE
51: cds.PROTECTIONID=cd.PROTECTIONID
52: AND cds.SOURCEID=srs.SOURCEID
53: AND cd.NAMEID=n.NAMEID
54: AND STOREDATE >= : p_StoreDateStart AND STOREDATE <= : p_StoreDateEnd
55: AND CONAME= : p_CompanyName
56: AND MATURITY= : p_Maturity
57: GROUP BY storedate';
58: OPEN ChartOutput FOR l_query;
59:
60: END;
61: END;
62: /
and now I receive an error saying:
ORA-00600 internal error code, arguments [15212],[4],[],[],[],[],[]....
Guys, do I miss something major here?
Again would appreciate your input.
ORA-00600 is a Oracle internal error (as it says) - so usually not your fault. Try raising a TAR on MetaLink - unless someone on this forum has seen it before . . .
"The power of instruction is seldom of much efficacy except in those happy dispositions where it is almost superfluous" - Gibbon, quoted by R.P.Feynman
Thanks for your feedback.
Just sorted it out.
One has to use a late binding here.
This one I've encountered myself once. The problem is your way of using bind variables. Rather try the following (late binding):
......
53: AND cd.NAMEID=n.NAMEID
54: AND STOREDATE >= :1 AND STOREDATE <= :2
55: AND CONAME= :3
56: AND MATURITY= :4
57: GROUP BY storedate';
58: OPEN ChartOutput FOR l_query USING p_StoreDateStart, p_StoreDateEnd, p_CompanyName, p_Maturity;
Bookmarks