Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 10th, 2012, 08:10 AM
amstel_za amstel_za is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 378 amstel_za User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 14 h 38 sec
Reputation Power: 10
Create Table Not Replicating SQL Select Results

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

Reply With Quote
  #2  
Old May 10th, 2012, 08:56 AM
amstel_za amstel_za is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 378 amstel_za User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 14 h 38 sec
Reputation Power: 10
Hey all,

Just an update to my previous post:

This command:

Code:
SELECT mn_rn.*,
  row_number() over (partition BY id order by lengthb(routenum) ASC, rteprior) trn
FROM MN_RN


orders the records correclty, but when I create a table of that select statement-the ordering is reversed and incorrect.

SO it appears the entire statement is being affected by the first SQL Select statment...

So the question is why the create table command doesn't replicate the SQL statement when using ROW_NUMBER OVER...

Last edited by amstel_za : May 10th, 2012 at 09:05 AM.

Reply With Quote
  #3  
Old May 11th, 2012, 11:43 AM
joeflinthiggins joeflinthiggins is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 14 joeflinthiggins User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 20 m 33 sec
Reputation Power: 0
You could try creating the table with a simple CREATE TABLE script, and then do an INSERT INTO/SELECT instead.

Reply With Quote
  #4  
Old May 11th, 2012, 01:45 PM
LKBrwn_DBA's Avatar
LKBrwn_DBA LKBrwn_DBA is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2006
Posts: 752 LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level)LKBrwn_DBA User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 3 h 5 m 12 sec
Reputation Power: 348
Cool

Quote:
Originally Posted by amstel_za
...Etc ... question is why the create table command doesn't replicate the SQL statement when using ROW_NUMBER OVER...

Oracle does NOT store the rows in any particular order.
__________________

Reply With Quote
  #5  
Old May 11th, 2012, 02:54 PM
clivew clivew is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 2,045 clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level)clivew User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 37 m
Reputation Power: 382
Following up on what LKBrwn_DBA explained:

Are you sure that the column results are truly being reversed?
Any chance you are not comparing the rows you think you are comparing?

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Create Table Not Replicating SQL Select Results

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap