Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# help with Group by clause Issue

Posted on 2011-03-11
Medium Priority
573 Views
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
Question by:paeddy
[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
• 2
• 2

LVL 41

Expert Comment

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;
``````
0

Author Comment

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 41

Accepted Solution

Sharath earned 1000 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

preraksheth earned 1000 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

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

## Featured Post

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
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.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
###### Suggested Courses
Course of the Month5 days, 15 hours left to enroll