i have table users information, sex, date of birth, "category", etc
i need take random sample, following results:
75% women
25% men
50% age between 25-35
40% age between 36-40
10% age between 41-60
to total of 2000
records.
is there anyway directly in mysql?
you cannot use order or limit within union statement in each sql queries. can used @ end.
so better use stored procedure output.
create procedure user_data_rand() begin create temporary table user_tmp id int, date_of_birth date, category string, age int) insert user_tmp select sex, date_of_birth, `category`,age users age between 25 , 35 , sex = 'male' order rand() limit 2000*0.5*0.25) ; insert user_tmp select sex, date_of_birth, `category`,age users age between 25 , 35 , sex = 'female' order rand() limit 2000*0.5*0.75) ; insert user_tmp select sex, date_of_birth, `category`,age users age between 36 , 40 , sex = 'male' order rand() limit 2000*0.40*0.25) ; insert user_tmp select sex, date_of_birth, `category`,age users age between 36 , 40 , sex = 'female' order rand() limit 2000*0.40*0.75) ; insert user_tmp select sex, date_of_birth, `category`,age users age between 41 , 60 , sex = 'male' order rand() limit 2000*0.10*0.25) ; insert user_tmp select sex, date_of_birth, `category`,age users age between 41 , 60 , sex = 'female' order rand() limit 2000*0.10*0.75) ; select * user_tmp; end
Comments
Post a Comment