1

I'm cleaning up SQL code from a previous engineer (not a programmer).

One query UNIONs the results to 2 almost identical queries, with an exactly identical sub-query, and the original code has a lot of "where" clauses (in both queries) to filter the data.

I am trying to use "with" tables to filter the data first, and then do the sub-queries and the union.

I keep getting a generic "end-of-file on communication channel" error during the "Prepare" step, but when I remove the DISTINCT clause from the sub-queries, it works - but it doesn't give me the results I need.

Here is the code I've "reduced" to show the error:

with

FilteredData as
(
    select
        ST.part
    ,   ST.order_No
    ,   ST.induct_Date
    ,   ST.complete_Date
    from
        Some_Table   ST
    where
    (
            ST.part     is not null
        and ST.order_No is not null
    )
    -- MUCH more filtering goes on here, to limit the number of records to look at
)
,
TempTable_01A as
(
    select
        FD.part
    ,   count( DISTINCT FD.part ) Count_1   -- The DISTINCT needs to be removed for it to compile
    ,   0                         Count_2
    ,   0                         AvgLengthOpen
    from
        FilteredData   FD
    where
            FD.induct_Date is not null
        and ( FD.induct_Date   >=  to_date( '01-01-2013', 'MM-DD-YYYY' )       )
        and ( FD.induct_Date   < ( to_date( '01-31-2013', 'MM-DD-YYYY' ) + 1 ) )
    group by
        FD.part
)
,
TempTable_01B as
(
    select
        FD.part
    ,   0                                        Count_1
    ,   count( DISTINCT FD.part )                Count_2   -- The DISTINCT needs to be removed for it to compile
    ,   avg( FD.complete_Date - FD.induct_Date ) AvgLengthOpen
    from
        FilteredData   FD
    where
            FD.complete_Date is not null
        and ( FD.complete_Date   >=  to_date( '01-01-2013', 'MM-DD-YYYY' )       )
        and ( FD.complete_Date   < ( to_date( '01-31-2013', 'MM-DD-YYYY' ) + 1 ) )
    group by
        FD.part
)
,
UnionTable as
(
    select
        TT_A.part
    ,   TT_A.Count_1
    ,   TT_A.Count_2
    ,   TT_A.AvgLengthOpen
    from
        TempTable_01A   TT_A

union

    select
        TT_B.part
    ,   TT_B.Count_1
    ,   TT_B.Count_2
    ,   TT_B.AvgLengthOpen
    from
        TempTable_01B   TT_B
)

select
    UT.part
,   max( UT.Count_1       ) MaxCount_1
,   max( UT.Count_2       ) MaxCount_2
,   max( UT.AvgLengthOpen ) MaxAvgLengthOpen
from
    UnionTable   UT
group by
    UT.part
order by
    1

NOTE: I am using Oracle SQL, version 10.0.2.1697. I get this same error whether I'm using PLSQL Developer, or my Perl program.

user272735
  • 10,473
  • 9
  • 65
  • 96
beschner
  • 11
  • 3
  • 3
    ORA-03113 errors are not the real errors. They mean that a bug caused Oracle to crash and it couldn't even send a proper error message. To find the real error message, have the DBA look at the alert log. There will probably be an ORA-00600 or ORA-00745 error message in there. Search for the first "[]" value on support.oracle.com and there will likely be a bug for the error that tells you how to fix it or work around it. – Jon Heller Sep 24 '13 at 02:01
  • 1
    @jonearles: Thanks for your reply. I know the "end-of-file" error is generic, but I didn't know how to find the root-problem. By your suggestion, I found my DBA, and we found Oracle bug 9002336 that deals with this problem. I was able to modify my code to set alter the session (alter session set "_with_subquery"=inline;) before executing this code. NOTE: setting the permanent fix (_optimizer_distinct_agg_transform = false) also worked, but probably would impact other users, so I can't use it in my environment. Thanks for your great response. – beschner Sep 24 '13 at 17:06

0 Answers0