Solved

Fetching random row from each group in oracle

Posted on 2013-06-06
3
1,191 Views
Last Modified: 2013-06-07
I have a table which has row, title, code and store as columns.

I have to group by code and store.

Assume table contains these rows.

Row        Title       Code        Store
      
1       title1      abcd        store1
2       title2      abcd        store1
3       title3      abcd        store1
4       title1      efgh        store2
5       titlex      efgh        store2

select code,store,count(*) from table
group by code,store order by count(*) desc;

It will give :

abcd        store1        3
efgh        store2    2

Now how can we get one random row from each group. For example it should give

Row       Title       Code        Store

2       title2       abcd            store1
5       titlex       efgh            store2

any one of the record from each group.

Regards,
Sasidhar.
0
Comment
Question by:sasidhar1229
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 450 total points
ID: 39226080
Try this:


select title, code, store from (
select title, code, store, row_number() over(partition by code, store order by dbms_random.value) myrn
from table
)
where myrn=1
/
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 50 total points
ID: 39226137
http://www.experts-exchange.com/Database/Oracle/A_9391-How-to-Create-User-Defined-Aggregates-in-Oracle.html


SELECT id, title, code, store
  FROM (SELECT t.*, random_value_agg(id) OVER (PARTITION BY store) rand_id
          FROM yourtable t)
 WHERE id = rand_id
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 39226446
Note, the random_value_agg method might be slightly more intuitive,
but it's not as efficient as the method in the first post.
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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
help on oracle query 5 43
exp/imp 25 73
PL SQL Search Across Columns 4 35
Select and Insert Query running slow 4 32
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…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
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.
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.

813 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

11 Experts available now in Live!

Get 1:1 Help Now