?
Solved

Oracle SQL - Not a GROUP BY Expression in Subquery

Posted on 2014-03-13
3
Medium Priority
?
2,202 Views
Last Modified: 2014-03-14
When I run the script below I bump into the following error message: ORA-00979: not a GROUP BY expression.
I'm trying to get a grant total for the current month (Month To Date Qty) per Item from the main query.


select
t.eplant_id,
t.class,
t.itemno,
t.rev,
t.descrip2,
sum (t.trans_quan) as "Total Qty",
t.unit,
bom.mfgcell,
                    (select
                    sum (t2.trans_quan)
                   
                    from
                    translog t2 left outer join arinvt inv2 on(t2.arinvt_id=inv2.id)
                    left outer join standard bom on (inv2.standard_id = bom.id)

                    where
                    t2.arinvt_id = t.arinvt_id
                    and to_char(t2.act_date,'YYYY/MM') = to_char(sysdate,'YYYY/MM')
                    and t2.trans_in_out = 'IN'
                    and t2.trans_type like '%DISP%'
                    and t2.trans_quan <> 0
                    and bom.mfgcell = 'SEWING-A'

                    group by
                    t2.itemno) "Month To Date Qty"


from
  translog t left outer join arinvt inv on(t.arinvt_id=inv.id)
  left outer join standard bom on (inv.standard_id = bom.id)

where
  t.act_date >= sysdate -0.5
  and t.trans_in_out = 'IN'
  and t.trans_type like '%DISP%'
  and t.trans_quan <> 0
  and bom.mfgcell = 'SEWING-A'

group by
t.eplant_id,
t.class,
t.itemno,
t.rev,
t.descrip2,
t.unit,
bom.mfgcell
0
Comment
Question by:brainsurf1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39926025
that correlated subquery needs an aggregate function - or - it also has to be part of the group by clause. Try this simple change (see line 10) first:
select 
t.eplant_id,
t.class, 
t.itemno, 
t.rev, 
t.descrip2,
sum (t.trans_quan) as "Total Qty",
t.unit,
bom.mfgcell,
                MAX (select 
                    sum (t2.trans_quan)
                   
                    from
                    translog t2 left outer join arinvt inv2 on(t2.arinvt_id=inv2.id)
                    left outer join standard bom on (inv2.standard_id = bom.id)

                    where
                    t2.arinvt_id = t.arinvt_id 
                    and to_char(t2.act_date,'YYYY/MM') = to_char(sysdate,'YYYY/MM')
                    and t2.trans_in_out = 'IN'
                    and t2.trans_type like '%DISP%'
                    and t2.trans_quan <> 0 
                    and bom.mfgcell = 'SEWING-A'

                    group by
                    t2.itemno) "Month To Date Qty"


from
  translog t left outer join arinvt inv on(t.arinvt_id=inv.id)
  left outer join standard bom on (inv.standard_id = bom.id)

where
  t.act_date >= sysdate -0.5
  and t.trans_in_out = 'IN'
  and t.trans_type like '%DISP%'
  and t.trans_quan <> 0 
  and bom.mfgcell = 'SEWING-A'

group by
t.eplant_id,
t.class, 
t.itemno, 
t.rev, 
t.descrip2,
t.unit,
bom.mfgcell

Open in new window

But I suspect that whole subquery can be done a different way.
0
 

Author Comment

by:brainsurf1
ID: 39926181
I Played around with aggregate function's and could not make this to work.
We need to figure out a alternative way to display the 'Month to date Qty'
0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 2000 total points
ID: 39926694
You could try something like this:
SELECT T.Eplant_Id
     , T.Class
     , T.Itemno
     , T.Rev
     , T.Descrip2
     , SUM ( CASE WHEN T.Act_Date >= SYSDATE - 0.5 
                  THEN T.Trans_Quan ELSE 0 END) AS "Total Qty"
     , T.Unit
     , Bom.Mfgcell
     , SUM ( T.Trans_Quan) "Month To Date Qty"
    FROM Translog T
         LEFT OUTER JOIN Arinvt Inv ON (T.Arinvt_Id = Inv.Id)
         LEFT OUTER JOIN STANDARD Bom ON (Inv.Standard_Id = Bom.Id)
   WHERE T.Act_Date >= TRUNC ( SYSDATE, 'Month')
     AND T.Trans_In_Out = 'IN'
     AND T.Trans_Type LIKE '%DISP%'
     AND T.Trans_Quan <> 0
     AND Bom.Mfgcell = 'SEWING-A'
GROUP BY T.Eplant_Id, T.Class, T.Itemno, T.Rev, T.Descrip2, T.Unit, Bom.Mfgcell

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question