Group By function in Oracle

Flex Tron
Flex Tron used Ask the Experts™
on
Hi Team,
I am trying to use a simple Group by code as shown, ...But don't need to do a Group BY on ISIN..(Varchar)...But when I remove that...I get error saying not a Groupby function...Can I just do a group by based on first two columns. ?


select distinct G.CLIENT_ID,G.YEARMONTH, D.ISIN,SUM(G.ACCOUNT_CBV) as ACCOUNT_CBV, SUM(G.TOTAL_REVENUES) as TOTAL_REVENUES from GMIS G, DEPOT D 
where G.CLIENT_ID = D.CLIENT_ID 
GROUP BY G.CLIENT_ID,G.YEARMONTH,D.ISIN order by Client_ID;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2012
Commented:
don't use distinct when doing a group by, it's redundant

if you don't care about the isin column, then it must be unique, in which case you can use MAX or MIN to include it in the aggregation.
or, if it's not unique but you don't care which value you return, then either MAX or MIN should be sufficient anyway.


  SELECT g.client_id,
         g.yearmonth,
         MAX(d.isin) isin,
         SUM(g.account_cbv) AS account_cbv,
         SUM(g.total_revenues) AS total_revenues
    FROM gmis g, depot d
   WHERE g.client_id = d.client_id
GROUP BY g.client_id, g.yearmonth
ORDER BY g.client_id;
IT Architect
Commented:
Hi,

maybe something like this:

select a.CLIENT_ID, a,YEARMONTH, a.ACCOUNT_CBV, a.TOTAL_REVENUES , b.ISIN
from (
select G.CLIENT_ID,G.YEARMONTH, SUM(G.ACCOUNT_CBV) as ACCOUNT_CBV, SUM(G.TOTAL_REVENUES) as TOTAL_REVENUES 
from GMIS G, DEPOT D 
where G.CLIENT_ID = D.CLIENT_ID 
GROUP BY G.CLIENT_ID,G.YEARMONTH
)a left join
(select distinct CLIENT_ID as CLIENT_ID, ISIN from DEPOT) b on a.CLIENT_ID=b.CLIENT_ID
 order by a.Client_ID;

Open in new window

Most Valuable Expert 2011
Top Expert 2012

Commented:
using two queries on the same table is needlessly expensive, even if there are indexes

also, using a left join changes the functionality of the query
Flex TronDeveloper

Author

Commented:
The Issue is that the tables have different levels of granularity. In that way, it's always challenging. Will keep researching on this...but sure the answers help a ton.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial