Avatar of xbox360dp
xbox360dp
 asked on

ORA-22818: subquery expressions not allowed here - Group BY

Gurus,

How do I get around the fact that I can't add a subquery to my group by clause and get the result I need?

SELECT kat.katkeyi,
  vgb.vgbbezc ,
  dok.dokkeyi,
  abp.abpbezc,
  dok.dokinfc,
  NVL (zplbezc, 'No TimePlan'),
  DOK.DOKOWNC,
  stragg (dok.doknlc),
(SELECT AFL.AFLFLDC
  FROM AFL
  INNER JOIN FTY
  ON AFL.AFLFTYKEYI  = FTY.FTYKEYI
  WHERE FTY.FTYBEZC  = 'LTtableID'
  AND AFL.AFLARTKEYI = ART.ARTKEYI
  AND AFL.AFLKAVKEYI = ART.ARTKAVKEYI
  ) AS lttableid
FROM kat, dok, vgb, abp, zpl
WHERE zpl.zplabpkeyi(+) = abp.abpkeyi
AND kat.katvgbkeyi      = vgb.vgbkeyi
AND dok.dokkatkeyi      = kat.katkeyi
GROUP BY kat.katkeyi, vgb.vgbbezc, dok.dokkeyi, abp.abpbezc, dok.dokinfc, zpl.zplbezc, DOK.DOKOWNC,
  (SELECT AFL.AFLFLDC
  FROM AFL
  INNER JOIN FTY
  ON AFL.AFLFTYKEYI  = FTY.FTYKEYI
  WHERE FTY.FTYBEZC  = 'LTtableID'
  AND AFL.AFLARTKEYI = ART.ARTKEYI
  AND AFL.AFLKAVKEYI = ART.ARTKAVKEYI);

ORA-22818: subquery expressions not allowed here
Oracle Database

Avatar of undefined
Last Comment
xbox360dp

8/22/2022 - Mon
Gerwin Jansen

Can you change the subquery in the group by to lttableid?
slightwv (䄆 Netminder)

You cannot use the alias in the same level that you create it in a group by.

Try this:

SELECT katkeyi,
  vgbbezc ,
  dokkeyi,
  abpbezc,
  dokinfc,
  zplbezc,
  DOKOWNC,
  stragg (dok.doknlc),
  lttableid
from (
SELECT kat.katkeyi,
  vgb.vgbbezc ,
  dok.dokkeyi,
  abp.abpbezc,
  dok.dokinfc,
  NVL (zplbezc, 'No TimePlan') zplbezc,
  DOK.DOKOWNC,
  dok.doknlc,
(SELECT AFL.AFLFLDC
  FROM AFL
  INNER JOIN FTY
  ON AFL.AFLFTYKEYI  = FTY.FTYKEYI
  WHERE FTY.FTYBEZC  = 'LTtableID'
  AND AFL.AFLARTKEYI = ART.ARTKEYI
  AND AFL.AFLKAVKEYI = ART.ARTKAVKEYI
  ) AS lttableid
FROM kat, dok, vgb, abp, zpl
WHERE zpl.zplabpkeyi(+) = abp.abpkeyi
AND kat.katvgbkeyi      = vgb.vgbkeyi
AND dok.dokkatkeyi      = kat.katkeyi
)
GROUP BY katkeyi, vgbbezc, dokkeyi, abpbezc, dokinfc, zplbezc, DOKOWNC, lttableid

Open in new window

xbox360dp

ASKER
Slightwv,

I get an error when I attempt to execute the SQL you posted.

ORA-00904: "DOKOWNC": invalid identifier
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
slightwv (䄆 Netminder)

Does DOKOWNC exist in the dok table?

I cannot ensure the SQL will run since I do not have your tables.
xbox360dp

ASKER
Yes ... it seems to error out of the whatever column is before "LTTABLEID".
slightwv (䄆 Netminder)

I did notice a small error but I don't think it is causing your error.

Change:
stragg (dok.doknlc),

to:
stragg (doknlc),
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
xbox360dp

ASKER
This didn't work.
Gerwin Jansen

>>This didn't work.
Post your error please, just saying something does not work isn't helping... You have one open question from slightwv:
Does DOKOWNC exist in the dok table?
xbox360dp

ASKER
Yes ... it seems to error out of the whatever column is before "LTTABLEID".

ORA-00904: "DOKOWNC": invalid identifier
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
slightwv (䄆 Netminder)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
xbox360dp

ASKER
Thanks for the effort slightwv!

I decided to take the subqueries out all together.