Hey all,
This is a very odd one...never encountered this before within SQL Developer
I have a SQL statement that returns a set of columns...but...when I create table as <SQL statement> I get the same columns but with 2 of the columns containing each others data, e.g:
SQL Select:
COL1 COL2 COL3 COL4
___________________________________
AND 10200000017805 CG-4 CG-3
Create Table as <SQL Select>:
COL1 COL2 COL3 COL4
___________________________________
AND 10200000017805 CG-3 CG-4
The SQL Select is correct and the Create Table As <SQL Select> is wrong.
Here is my SQL:
Code:
Create table ALTERNATENUMBERS as
SELECT ctry,
id,
MAX(DECODE(tp,'EN', RN)) EN,
MAX(DECODE(tp,'RN', RN)) RN,
MAX(DECODE(tp,'AN', RN)) AN
FROM
(SELECT *
FROM
(SELECT ctry,
id,
tp,
trn,
listagg(routenum, '/') within GROUP (
ORDER BY routenum) over (partition BY id, tp) RN
FROM
(SELECT ctry,
id,
routenum,
rteprior,
trn,
CASE
WHEN rtetyp= 5
THEN 'EN'
WHEN (rtetyp <> 5
AND trn =1)
THEN 'RN'
ELSE 'AN'
END AS tp
FROM
(SELECT mn_rn.*,
row_number() over (partition BY id order by lengthb(routenum) ASC, rteprior) trn
FROM MN_RN
)
)
)
GROUP BY ctry,
id,
tp,
rn,
trn
ORDER BY ctry,
id,
tp,
rn,
trn
)
GROUP BY ctry,
id
ORDER BY ctry,
id
Unfortunately I cannot give you any data (too much of it) and small scale testing works, it's only when I run it on the 11million records do I get some (not all), just some of the data being mixed up between columns.
Now, I've tried:
1. Using SQLPLus - no joy
2. Creating the Table and then inserting the data into a blank table - also no joy
3. Using a VIEW - no joy, listagg doesn't work in VIEW tables
It's very odd and I do understand that without data it's hard to replicate the issue but does anyone have any ideas as to why this statement works as a SELECT but when written to a table has data anomolies ?
Regards,
Ben