简体   繁体   中英

SQL randomly selecting rows based on a certain column

I have a big table in which one of the columns has customer IDs. I want to randomly select 3% of the customers and their records. Each customers could have thousands of records/rows. So, here, the certain column is customer ID.

Here is how I did it but it is not correct:

Select *
FROM
  my_table group by customer_iD
WHERE
  rand() < 0.3 
;

One method is:

select t.*
from mytable t
where t.customer_id in (select t2.customer_id
                        from mytable t2
                        group by t2.customer_id
                        having rand() < 0.3
                       );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM