Home » SQL & PL/SQL » SQL & PL/SQL » a column may not be outer-joined to a subquery
a column may not be outer-joined to a subquery [message #644205] Fri, 30 October 2015 04:54 Go to next message
javon13
Messages: 17
Registered: October 2015
Junior Member
I have some special select which works until I do insert..select.
When I run insert..select it gives me:
SQL Error: ORA-01799: "a column may not be outer-joined to a subquery"

The truth is, I'm short with ideas how to repair it and why it works as plain select but not as insert..select.

It selects data from table A, inner join with table B. On top of that it left join a table with values for translation of values from table B
Here:
--SQL--
insert into TARGET_TABLE (
 ID,
 NAM1,
 NAM2,
 ADR1,
 DPT,
 RETURN_CODE1,
 RETURN_CODE2
)
select
  TAB_A.ID ID,
  TAB_A.NAM1 NAM1,
  TAB_A.NAM2 NAM2,
  TAB_A.ADR1 ADR1,
  TAB_B.DPT DPT,
  VALTB.RETURN_CODE1 RETURN_CODE1,
  VALTB.RETURN_CODE2 RETURN_CODE2
FROM TAB_A
  INNER JOIN TAB_B
    ON TAB_A.ID = TAB_B.NAME
  LEFT JOIN ( --THIS IS BEGINNING OF PROBLEMATIC PART
    SELECT
      NVL(VALTB1.RETURN_CODE1, (SELECT RETURN_CODE1 FROM VAL_TABLE WHERE PAR2='*' AND PAR3='*' AND PAR1='*')) RETURN_CODE1,
      NVL(VALTB1.RETURN_CODE2, (SELECT RETURN_CODE2 FROM VAL_TABLE WHERE PAR2='*' AND PAR3='*' AND PAR1='*')) RETURN_CODE2,
      VALTB1.PAR1, VALTB1.PAR2, VALTB1.PAR3
      from VAL_TABLE VALTB1
    ) VALTB 
    on   (case when TAB_B.PAR1 in (select VALTB2.PAR1 from VAL_TABLE VALTB2)
		       then TAB_B.PAR1
            else '*' 
          end
        = VALTB.PAR1)
     and (case when TAB_B.PAR2 in (select VALTB2.PAR2 
                                     from VAL_TABLE VALTB2
                                     where VALTB2.PAR1 = (case when TAB_B.PAR1 in (select VALTB2.PAR1 
                                                                                     from VAL_TABLE VALTB2) 
                                                               then TAB_B.PAR1
                                                               else '*'
                                                          end))
               then TAB_B.PAR2
               else '*' 
          end
        = VALTB.PAR2)
     and (case when TAB_B.PAR3 in (select VALTB2.PAR3 
                                     from VAL_TABLE VALTB2
                                     where VALTB2.PAR1 = (case when TAB_B.PAR1 in (select VALTB2.PAR1 
                                                                                     from VAL_TABLE VALTB2) 
                                                               then TAB_B.PAR1
                                                               else '*'
                                                          end)
                                        and VALTB2.PAR2 = (case when TAB_B.PAR2 in (select VALTB2.PAR2 
                                                                                      from VAL_TABLE VALTB2
                                                                                      where VALTB2.PAR1 = (case when TAB_B.PAR1 in (select VALTB2.PAR1 
                                                                                                                                      from VAL_TABLE VALTB2) 
                                                                                                                then TAB_B.PAR1
                                                                                                                else '*'
                                                                                                           end))
                                                                 then TAB_B.PAR2
                                                                 else '*'
                                                            end ))
               then TAB_B.PAR3
               else '*' 
        end
      = VALTB.PAR3);
--SQL END--

Thanks a lot in advance for your hints. If you'd like to test it, I attached a file with complete environment: tables, data, select. Select is properly formatted in the file.
*BlackSwan added {code} tags. Please do so yourself in the future.
How to use {code} tags and make your code easier to read
  • Attachment: orafag.sql
    (Size: 4.94KB, Downloaded 1448 times)

[Updated on: Fri, 30 October 2015 07:40] by Moderator

Report message to a moderator

Re: a column may not be outer-joined to a subquery [message #644211 is a reply to message #644205] Fri, 30 October 2015 09:45 Go to previous messageGo to next message
sandeep_orafaq
Messages: 88
Registered: September 2014
Member
Your script has lots of errors, Please give proper data sets.

Added:
You said your select statement is working, well , i don't think so.

[Updated on: Fri, 30 October 2015 09:54]

Report message to a moderator

Re: a column may not be outer-joined to a subquery [message #644212 is a reply to message #644211] Fri, 30 October 2015 10:06 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
post results from SQL below
select COUNT(*)
FROM TAB_A
  INNER JOIN TAB_B
    ON TAB_A.ID = TAB_B.NAME
  LEFT JOIN ( --THIS IS BEGINNING OF PROBLEMATIC PART
    SELECT
      NVL(VALTB1.RETURN_CODE1, (SELECT RETURN_CODE1 FROM VAL_TABLE WHERE PAR2='*' AND PAR3='*' AND PAR1='*')) RETURN_CODE1,
      NVL(VALTB1.RETURN_CODE2, (SELECT RETURN_CODE2 FROM VAL_TABLE WHERE PAR2='*' AND PAR3='*' AND PAR1='*')) RETURN_CODE2,
      VALTB1.PAR1, VALTB1.PAR2, VALTB1.PAR3
      from VAL_TABLE VALTB1
    ) VALTB 
    on   (case when TAB_B.PAR1 in (select VALTB2.PAR1 from VAL_TABLE VALTB2)
		       then TAB_B.PAR1
            else '*' 
          end
        = VALTB.PAR1)
     and (case when TAB_B.PAR2 in (select VALTB2.PAR2 
                                     from VAL_TABLE VALTB2
                                     where VALTB2.PAR1 = (case when TAB_B.PAR1 in (select VALTB2.PAR1 
                                                                                     from VAL_TABLE VALTB2) 
                                                               then TAB_B.PAR1
                                                               else '*'
                                                          end))
               then TAB_B.PAR2
               else '*' 
          end
        = VALTB.PAR2)
     and (case when TAB_B.PAR3 in (select VALTB2.PAR3 
                                     from VAL_TABLE VALTB2
                                     where VALTB2.PAR1 = (case when TAB_B.PAR1 in (select VALTB2.PAR1 
                                                                                     from VAL_TABLE VALTB2) 
                                                               then TAB_B.PAR1
                                                               else '*'
                                                          end)
                                        and VALTB2.PAR2 = (case when TAB_B.PAR2 in (select VALTB2.PAR2 
                                                                                      from VAL_TABLE VALTB2
                                                                                      where VALTB2.PAR1 = (case when TAB_B.PAR1 in (select VALTB2.PAR1 
                                                                                                                                      from VAL_TABLE VALTB2) 
                                                                                                                then TAB_B.PAR1
                                                                                                                else '*'
                                                                                                           end))
                                                                 then TAB_B.PAR2
                                                                 else '*'
                                                            end ))
               then TAB_B.PAR3
               else '*' 
        end
      = VALTB.PAR3);
Re: a column may not be outer-joined to a subquery [message #644214 is a reply to message #644211] Fri, 30 October 2015 11:18 Go to previous messageGo to next message
javon13
Messages: 17
Registered: October 2015
Junior Member
Thanks for checking.
I have run the script in SQL developer and it works. Could you please tell me what errors are you getting?
Re: a column may not be outer-joined to a subquery [message #644215 is a reply to message #644212] Fri, 30 October 2015 11:20 Go to previous messageGo to next message
javon13
Messages: 17
Registered: October 2015
Junior Member
Thanks.
The result is 4.

1	Alpha	Smith	Ct	D100	1	10
2	Beta	Johnson	Nv	D200	2	20
3	Gamma	Newman	Ca	D300	6	60
4	Delta	Doe	Co	D400	(null)	(null)

[Updated on: Fri, 30 October 2015 11:22]

Report message to a moderator

Re: a column may not be outer-joined to a subquery [message #644216 is a reply to message #644214] Fri, 30 October 2015 11:22 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

As we have not your tables there will be numerous.
So post the CREATE TABLE statements for these ones.

Re: a column may not be outer-joined to a subquery [message #644217 is a reply to message #644216] Fri, 30 October 2015 11:23 Go to previous messageGo to next message
javon13
Messages: 17
Registered: October 2015
Junior Member
My tables are in the file in the original post. There are create table statements, insert all and select itself.
Re: a column may not be outer-joined to a subquery [message #644219 is a reply to message #644217] Fri, 30 October 2015 11:25 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Many of us can't or don't want to download files so post them inline (I mean just the CREATE TABLE statements as your problem seems to be syntactical).

[Updated on: Fri, 30 October 2015 11:26]

Report message to a moderator

Re: a column may not be outer-joined to a subquery [message #644220 is a reply to message #644219] Fri, 30 October 2015 11:28 Go to previous messageGo to next message
javon13
Messages: 17
Registered: October 2015
Junior Member
All right, I didn't want to make a mess in posts, but you're right.
Here it is:

create table TAB_A (
  ID INTEGER NOT NULL,
  NAM1 VARCHAR2(20) NOT NULL,
  NAM2 VARCHAR2(40) NOT NULL,
  ADR1 VARCHAR2(50) NOT NULL,

  CONSTRAINT PK_TAB_A PRIMARY KEY (ID)
);

create table TAB_B (
  NAME INTEGER NOT NULL,
  DPT VARCHAR2(30) NOT NULL,
  PAR1 VARCHAR2(10) NOT NULL,
  PAR2 VARCHAR2(10) NOT NULL,
  PAR3 VARCHAR2(10) NOT NULL,
  CONSTRAINT PK_TAB_B PRIMARY KEY (NAME)
);

create table VAL_TABLE (
  PAR1 VARCHAR2(10) NOT NULL,
  PAR2 VARCHAR2(10) NOT NULL,
  PAR3 VARCHAR2(10) NOT NULL,
  RETURN_CODE1 INTEGER NOT NULL,
  RETURN_CODE2 INTEGER NOT NULL,
  SEQ INTEGER NOT NULL
);

CREATE UNIQUE INDEX UNQ_VAT_TABLE_PAR ON VAL_TABLE (PAR1, PAR2, PAR3);

create table TARGET_TABLE (
  ID INTEGER NOT NULL,
  NAM1 VARCHAR2(20) NOT NULL,
  NAM2 VARCHAR2(40) NOT NULL,
  ADR1 VARCHAR2(50) NOT NULL,
  DPT VARCHAR2(30) NOT NULL,
  RETURN_CODE1 INTEGER NOT NULL,
  RETURN_CODE2 INTEGER NOT NULL,

  CONSTRAINT PK_TARGET_TABLE PRIMARY KEY (ID)
);


INSERT ALL
  INTO TAB_A (ID, NAM1, NAM2, ADR1) VALUES (1, 'Alpha', 'Smith', 'Ct')
  INTO TAB_A (ID, NAM1, NAM2, ADR1) VALUES (2, 'Beta', 'Johnson', 'Nv')
  INTO TAB_A (ID, NAM1, NAM2, ADR1) VALUES (3, 'Gamma', 'Newman', 'Ca')
  INTO TAB_A (ID, NAM1, NAM2, ADR1) VALUES (4, 'Delta', 'Doe', 'Co')

  INTO TAB_B (NAME, DPT, PAR1, PAR2, PAR3) VALUES (1, 'D100', 'S', 'D', 'BDL')
  INTO TAB_B (NAME, DPT, PAR1, PAR2, PAR3) VALUES (2, 'D200', 'T', 'G', 'LAS')
  INTO TAB_B (NAME, DPT, PAR1, PAR2, PAR3) VALUES (3, 'D300', 'R', 'F', 'LAX')
  INTO TAB_B (NAME, DPT, PAR1, PAR2, PAR3) VALUES (4, 'D400', 'S', 'E', 'DEN')

  INTO VAL_TABLE (PAR1, PAR2, PAR3, RETURN_CODE1, RETURN_CODE2, SEQ) VALUES ('S', '*', 'BDL', 1, 10, 1)
  INTO VAL_TABLE (PAR1, PAR2, PAR3, RETURN_CODE1, RETURN_CODE2, SEQ) VALUES ('T', 'G', 'LAS', 2, 20, 1)
  INTO VAL_TABLE (PAR1, PAR2, PAR3, RETURN_CODE1, RETURN_CODE2, SEQ) VALUES ('T', '*', 'LAX', 3, 30, 1)
  INTO VAL_TABLE (PAR1, PAR2, PAR3, RETURN_CODE1, RETURN_CODE2, SEQ) VALUES ('*', 'D', 'DEN', 4, 40, 1)
  INTO VAL_TABLE (PAR1, PAR2, PAR3, RETURN_CODE1, RETURN_CODE2, SEQ) VALUES ('*', 'E', '*', 5, 50, 1)
  INTO VAL_TABLE (PAR1, PAR2, PAR3, RETURN_CODE1, RETURN_CODE2, SEQ) VALUES ('R', 'F', '*', 6, 60, 1)
  INTO VAL_TABLE (PAR1, PAR2, PAR3, RETURN_CODE1, RETURN_CODE2, SEQ) VALUES ('*', '*', '*', 7, 70, 99)
SELECT 1 FROM DUAL;
Re: a column may not be outer-joined to a subquery [message #644257 is a reply to message #644220] Mon, 02 November 2015 01:54 Go to previous messageGo to next message
javon13
Messages: 17
Registered: October 2015
Junior Member
Ok, I've spent couple of more hours with trials and errors and I'm starting to think about rewriting it completely and solve the translation by a function. But I'm concerned about possible lost of speed so this holds me still on the original track.
Do you think is that possible to solve it by a plain sql?
Re: a column may not be outer-joined to a subquery [message #644271 is a reply to message #644257] Mon, 02 November 2015 07:03 Go to previous message
javon13
Messages: 17
Registered: October 2015
Junior Member
I gave up and rewrote it completely without clever logic Smile
Now I'm just using case-when-then-else and values are hard-coded not in table of values.
Many thanks to those who participated.
Previous Topic: case statement
Next Topic: i want to upload a file in database using plsql
Goto Forum:
  


Current Time: Sun Jul 05 10:11:53 CDT 2026