Solved

help with Group by clause Issue

Posted on 2011-03-11
5
540 Views
Last Modified: 2012-05-11
HI,
     I am trying to group by the PSNmb as in the query 1. It works fine.
PSNmb and PSeqNmb are in the same table b. PSeqNmb is the Primary key on table b.
 Each PSNmb has a unique PSeqNmb assigned to it. (so technically, PSNmb and PSeqNmb which ever is if referred, tells about the same info.) The query is actually big, which produces the same results and the same issue, I just truncated the query to be easy.
If I use the query 2, I get that 'ORA-00979: not a GROUP BY expression'.
I am trying to understand the reason behind this. Ideally, two grouping would provide the same grouping results I thought.
Can anyone explain the difference between these two scenarios? This would help me for other queries where I use group by clause.

query 1:
select  b.PSNmb ,
        (select sum(x.Amount *
       (((CASE WHEN x.FCd = 'A' then 1 else 0 end)
       - (CASE WHEN x.FCd = 'B' then 1 else 0 end)) ))
        from TranTbl x
        where x.AceNmb = b.PSNmb )
    from PSTbl b
    where b.PtId =  @PtId
    group by b.PSNmb ;

query 2:
select  b.PSeqNmb,
        (select sum(x.Amount*
       (((CASE WHEN x.FCd = 'A' then 1 else 0 end)
       - (CASE WHEN x.FCd = 'B' then 1 else 0 end)) ))
        from TranTbl x
        where x.AceNmb = b.PSNmb )
    from PSTbl b
    where b.PtId =  @PtId
    group by b.PSeqNmb;
0
Comment
Question by:paeddy
  • 2
  • 2
5 Comments
 
LVL 40

Expert Comment

by:Sharath
ID: 35113508
You have join condition on PSNmb in the sub-query. Change it to PSeqNmb and try.
select  b.PSeqNmb,
        (select sum(x.Amount*
       (((CASE WHEN x.FCd = 'A' then 1 else 0 end)
       - (CASE WHEN x.FCd = 'B' then 1 else 0 end)) ))
        from TranTbl x 
        where x.AceNmb = b.PSeqNmb ) 
    from PSTbl b
    where b.PtId =  @PtId 
    group by b.PSeqNmb;

Open in new window

0
 

Author Comment

by:paeddy
ID: 35113537
Thank you very much.  Yes it works if I use the PSeqNmb.
It also works if I modify the query like below:
select  b.PSNmb ,
      SUM(  (select sum(x.Amount *
       (((CASE WHEN x.FCd = 'A' then 1 else 0 end)
       - (CASE WHEN x.FCd = 'B' then 1 else 0 end)) ))
        from TranTbl x
        where x.AceNmb = b.PSNmb ) )
    from PSTbl b
    where b.PtId =  @PtId
    group by b.PSNmb ;

I was trying to understand the difference. can you explain to me please?
0
 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
ID: 35113588
The reason is you are grouping the result set based on PSeqNmb. So you will be having unique PSeqNmbs in the result set. When you JOIN this result set with another table, it expects the same column as the JOIN condition.
If you try to join on a diferent column, it does not know which value to be picked for JOIN condition among the set of records grouped by PSeqNmb.on a different column (PSNmb).Eventhough you have one record for every group (PSeqNmb), it won't accept. you have to apply aggrgate functions on the non-group by columns.
0
 
LVL 2

Assisted Solution

by:preraksheth
preraksheth earned 250 total points
ID: 35114708
The difference is that in the second query, Oracle does not know what to do with (potential) differnt values of PSeqNmb for the same value of PSNmb  (and it will throw this error even if you have one to one relation between these two fields, because it fails on the parse state and does not even go to execution state)
0
 

Author Closing Comment

by:paeddy
ID: 35118759
Thank you very much guys for clearing this issue.
Its a great help for me to avoid confusion..
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
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 shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now