jeudi 21 octobre 2021

sampling groups instead of rows

I want to randomly select based on group ids (ColA) instead of rows. Let's say my inital table is this:

+---------------------+
|ColA   | ColB | ColC |
+---------------------+
| AA       C        5 |      
| AA       C        8 |     
| AA       C        9 |      
| BB       D        3 |      
| BB       D        4 |    
| CC       E        1 |    
| CC       E        2 |     
| CC       E        3 |     
| CC       E        5 |    
+---------------------+

I want to get something like this:

+---------------------+
 ColA  | ColB |   ColC 
+---------------------+
| AA       C        5 |      
| AA       C        8 |     
| AA       C        9 |    
| CC       E        1 |      
| CC       E        2 |      
| CC       E        3 |     
| CC       E        5 |      
+---------------------+

So let's say i want to get whole groupings from Col A randomly. I have done this but it gives me random rows rather than groups

SELECT distinct  ColA,ColB, ColC FROM TABLE
ORDER BY  RAND()
LIMIT 20000

I'd appreciate any clues on how to implement it.




Aucun commentaire:

Enregistrer un commentaire