1

I have a query that looks similar to the one below (albeit more complicated). When running it I get the following error: ORA-22818: Subquery expressions not allowed here in my group by statement.

What is the best way for me to get around this issue?

SELECT table1.ID   
       NVL(fget_office(fget_last_catc_id_by_date((SELECT MAX(table3.date) FROM table3 INNER JOIN table1 ON table1.ID = table3.id),table1.NUM), fget_max_split_line_no('FILL',(SELECT MAX table3.tc_id) FROM table3 INNER JOIN table1 ON table1.ID = table3.ID INNER JOIN table4 ON table3.tc_id = table3.tc_id))), table1.distribution) "OFFICE", --eper.DISTRIBUTION "OFFICE",
       table1.name 
  FROM table1
  LEFT JOIN table2
    ON table1.ID = table2.ID 
 WHERE table1.company in ('CP01', 'CP02')
 GROUP BY table1.ID,
          NVL(fget_office(fget_last_catc_id_by_date((SELECT MAX(table3.date) FROM table3 INNER JOIN table1 ON table1.ID = table3.id),table1.NUM), fget_max_split_line_no('FILL',(SELECT MAX table3.tc_id) FROM table3 INNER JOIN table1 ON table1.ID = table3.ID INNER JOIN table4 ON table3.tc_id = table3.tc_id))), table1.distribution),          
          table1.name

2 Answers 2

2

Your code sample looks like you're using GROUP BY just to pull distinct rows. In that case, try this:

SELECT DISTINCT
       table1.ID   
       NVL(fget_office(fget_last_catc_id_by_date((SELECT MAX(table3.date) FROM table3 INNER JOIN table1 ON table1.ID = table3.id),table1.NUM), fget_max_split_line_no('FILL',(SELECT MAX table3.tc_id) FROM table3 INNER JOIN table1 ON table1.ID = table3.ID INNER JOIN table4 ON table3.tc_id = table3.tc_id))), table1.distribution) "OFFICE", --eper.DISTRIBUTION "OFFICE",
       table1.name 
  FROM table1
  LEFT JOIN table2
    ON table1.ID = table2.ID 
 WHERE table1.company in ('CP01', 'CP02')

In case you really are doing aggregation in your "real" query, a quick workaround would be to use a Common Table Expression (CTE), which is supported in Oracle 9i. This example assumes you're summing a column named some_value:

WITH x AS (
  SELECT table1.ID   
         NVL(fget_office(fget_last_catc_id_by_date((SELECT MAX(table3.date) FROM table3 INNER JOIN table1 ON table1.ID = table3.id),table1.NUM), fget_max_split_line_no('FILL',(SELECT MAX table3.tc_id) FROM table3 INNER JOIN table1 ON table1.ID = table3.ID INNER JOIN table4 ON table3.tc_id = table3.tc_id))), table1.distribution) "OFFICE", --eper.DISTRIBUTION "OFFICE",
         table1.name,
         some_value
    FROM table1
    LEFT JOIN table2
      ON table1.ID = table2.ID 
   WHERE table1.company in ('CP01', 'CP02')
)
SELECT ID, OFFICE, name, SUM(some_value)
FROM x
GROUP BY ID, Office, name
1

It looks to me like the results of those functions are directly or indirectly determined by the values of table1.

If so, you can perform the distinct operation on a simple set of data from table1 and table2 and apply the functions afterwards. This would reduce the number of calls to the functions and improve efficiency.

with cte1 as (
  select
    table1.id   
    table1.num
    table1.distribution,
    table1.name 
  from
    table1 left join table2 on (table1.id = table2.id)
 where
   table1.company in ('CP01', 'CP02'))
select
  cte1.id,
  coalesce(
    fget_office(
      fget_last_catc_id_by_date(
        (select max(table3.date)
         from   table3 inner join cte1 on cte1.id = table3.id),
        cte1.num),
      fget_max_split_line_no(
        'FILL',
        (select max(table3.tc_id)
         from   table3 inner join cte1   on cte1.id      = table3.id
                       inner join table4 on table3.tc_id = table3.tc_id))),
    table1.distribution) office
  cte1.name 
from cte1
/

You might as well get used to using Coalesce() instead of Nvl() -- it's ANSI compliant, more flexible, and features short-circuit evaluation so it's handy of your codebase has a lot of PL/SQL functions that get called in SQL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.