Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Picking only one record when the values are distinct

866081Apr 16 2016 — edited Apr 17 2016

Team,

I have a column called STATE and I need to pick the value based on the number of occurrence of that particular value.

Eg:

 

IDSTATE
1OH
1SD
1OH
1SD
1

OH

In this Eg I should be picking OH as it is occuring 3 times which is the max.

I use the below query to get this

SELECT ID,STATE FROM (

SELECT  ID,STATE,

RANK() OVER (PARTITION BY ID ORDER BY CNT desc) AS TRANK FROM ( 

SELECT R.ID,R.STATE,

COUNT(*) CNT FROM

OPMASTER.DTL R

GROUP BY R.ID,R.STATE))

WHERE TRANK=1

But am facing problem when the value is unique

 

IDSTATE
1PA
1NY

In this case since the values are unique , RANK is pulling both the values instead of one.In this case when the values are distinct I need to pull only latest ROWID. But am not able to fit in ROW ID in the above query

Any help is appreciated. Thanks

This post has been answered by Frank Kulash on Apr 16 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 15 2016
Added on Apr 16 2016
3 comments
1,622 views