dbTalk Databases Forums  

Random sample by subgroup

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Random sample by subgroup in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
PMDORAIS@gmail.com
 
Posts: n/a

Default Random sample by subgroup - 10-30-2006 , 01:02 PM






Hi,

First, i'm a newbie with sql and i would like to do the following:

I want to extract a random sample for each value of a certain field in
a table;


For example


table

clientid name group
1 xxx a
2 yyy b
3 ccc c

etc...

and i would like a sample of 20% of the records for each group (a, b,
c).... how can i do that...??

thanks!


Reply With Quote
  #2  
Old   
Michel Cadot
 
Posts: n/a

Default Re: Random sample by subgroup - 10-30-2006 , 02:09 PM







<PMDORAIS (AT) gmail (DOT) com> a écrit dans le message de news: 1162234976.272547.259500 (AT) h48g20...oglegroups.com...
Quote:
Hi,

First, i'm a newbie with sql and i would like to do the following:

I want to extract a random sample for each value of a certain field in
a table;


For example


table

clientid name group
1 xxx a
2 yyy b
3 ccc c

etc...

and i would like a sample of 20% of the records for each group (a, b,
c).... how can i do that...??

thanks!

Something like (GROUP and NAME are reserved words don't use them):

with
data as (
select clientid, nom, groupe,
row_number () over (partition by groupe order by dbms_random.value) rn,
count(*) over (partition by groupe) cnt
from mytable
)
select clientid, nom, groupe
from data
where rn <= 0.2 * cnt
/

Regards
Michel Cadot





Reply With Quote
  #3  
Old   
PMDORAIS@gmail.com
 
Posts: n/a

Default Re: Random sample by subgroup - 10-30-2006 , 02:50 PM



Great!

You saved my life!!

Merci Michel!!

Pierre

Michel Cadot a écrit :

Quote:
PMDORAIS (AT) gmail (DOT) com> a écrit dans le message de news: 1162234976.272547..259500 (AT) h48g2000cwc (DOT) googlegroups.com...
| Hi,
|
| First, i'm a newbie with sql and i would like to do the following:
|
| I want to extract a random sample for each value of a certain field in
| a table;
|
|
| For example
|
|
| table
|
| clientid name group
| 1 xxx a
| 2 yyy b
| 3 ccc c
|
| etc...
|
| and i would like a sample of 20% of the records for each group (a, b,
| c).... how can i do that...??
|
| thanks!
|

Something like (GROUP and NAME are reserved words don't use them):

with
data as (
select clientid, nom, groupe,
row_number () over (partition by groupe order by dbms_random.value) rn,
count(*) over (partition by groupe) cnt
from mytable
)
select clientid, nom, groupe
from data
where rn <= 0.2 * cnt
/

Regards
Michel Cadot


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.