Solved

Oracle SQL - Not a GROUP BY Expression in Subquery

Posted on 2014-03-13
3
2,072 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

696 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