dbTalk Databases Forums  

Select random rows to populate

comp.databases.mysql comp.databases.mysql


Discuss Select random rows to populate in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
noydb
 
Posts: n/a

Default Select random rows to populate - 06-08-2011 , 09:30 AM






What would the SQL statement be to randomly select 30% of the records
in table2 and calculate column X equal to string 'test' for just those
random records? I just have no idea how to get the random set of
records. How?

Thanks for any help!

Reply With Quote
  #2  
Old   
Luuk
 
Posts: n/a

Default Re: Select random rows to populate - 06-08-2011 , 02:39 PM






On 08-06-2011 16:30, noydb wrote:
Quote:
What would the SQL statement be to randomly select 30% of the records
in table2 and calculate column X equal to string 'test' for just those
random records? I just have no idea how to get the random set of
records. How?

Thanks for any help!
A select statement would look something like:

select t1.i, t1.t
from testText t1 , (select count(*) c
from testText) t2
where rand()*c < t2.c*0.30;

But it dows not select exactly 30%.....

--
Luuk

Reply With Quote
  #3  
Old   
J.O. Aho
 
Posts: n/a

Default Re: Select random rows to populate - 06-08-2011 , 02:40 PM



noydb wrote:
Quote:
What would the SQL statement be to randomly select 30% of the records
in table2 and calculate column X equal to string 'test' for just those
random records? I just have no idea how to get the random set of
records. How?

Thanks for any help!
This gives you the randomness:
select * from table order by rand()


--

//Aho

Reply With Quote
  #4  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Select random rows to populate - 06-08-2011 , 03:15 PM



J.O. Aho wrote:
Quote:
noydb wrote:
What would the SQL statement be to randomly select 30% of the records
in table2 and calculate column X equal to string 'test' for just those
random records? I just have no idea how to get the random set of
records. How?

Thanks for any help!

This gives you the randomness:
select * from table order by rand()


really?

Not seen that one..

then a limit statement formed as a subquery from a count of the rows
sorts the rest..


MM that's neat

select id from day order by rand() limit 10;
+------+
Quote:
id |
+------+
399 |
1899 |
1041 |
516 |
1510 |
2212 |
1461 |
1177 |
3137 |
1439 |
+------+
10 rows in set (0.04 sec)


the problem is that whilst establishing the number of 30% rows is easy
enough..

mysql> (select floor(count(id)*0.3) as size from day);
+------+
Quote:
size |
+------+
1049 |
+------+

...mysql wont take the output from a subquery as an argument t limit..

mysql> select id from day order by rand()
limit (select floor(count(id)*0.3) as size from day);
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '(select floor(count(id)*0.3) as size from day)' at
line 1
mysql>

Reply With Quote
  #5  
Old   
J.O. Aho
 
Posts: n/a

Default Re: Select random rows to populate - 06-09-2011 , 02:43 PM



The Natural Philosopher wrote:

Quote:
the problem is that whilst establishing the number of 30% rows is easy
enough..

mysql> (select floor(count(id)*0.3) as size from day);
+------+
| size |
+------+
| 1049 |
+------+

..mysql wont take the output from a subquery as an argument t limit..

mysql> select id from day order by rand()
limit (select floor(count(id)*0.3) as size from day);
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '(select floor(count(id)*0.3) as size from day)' at
line 1
mysql
Yeah, you are so right about that, I even tried with a CREATE FUNCTION to make
it to work, but it didn't like my limit with a variable.

--

//Aho

Reply With Quote
  #6  
Old   
The Natural Philosopher
 
Posts: n/a

Default Re: Select random rows to populate - 06-09-2011 , 02:55 PM



J.O. Aho wrote:
Quote:
The Natural Philosopher wrote:

the problem is that whilst establishing the number of 30% rows is easy
enough..

mysql> (select floor(count(id)*0.3) as size from day);
+------+
| size |
+------+
| 1049 |
+------+

..mysql wont take the output from a subquery as an argument to limit..

mysql> select id from day order by rand()
limit (select floor(count(id)*0.3) as size from day);
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '(select floor(count(id)*0.3) as size from day)' at
line 1
mysql

Yeah, you are so right about that, I even tried with a CREATE FUNCTION to make
it to work, but it didn't like my limit with a variable.

sounds like you need to do it in two stages using a higher level
language, then.

Reply With Quote
  #7  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Select random rows to populate - 06-11-2011 , 01:05 AM



On 2011-06-09 21:55, The Natural Philosopher wrote:
Quote:
J.O. Aho wrote:
The Natural Philosopher wrote:

the problem is that whilst establishing the number of 30% rows is easy
enough..

mysql> (select floor(count(id)*0.3) as size from day);
+------+
| size |
+------+
| 1049 |
+------+

..mysql wont take the output from a subquery as an argument to limit..

mysql> select id from day order by rand()
limit (select floor(count(id)*0.3) as size from day);
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '(select floor(count(id)*0.3) as size from day)' at
line 1
mysql

Yeah, you are so right about that, I even tried with a CREATE FUNCTION to make
it to work, but it didn't like my limit with a variable.

sounds like you need to do it in two stages using a higher level
language, then.

It's possible (but probably inefficient) to mimic the row_number() olap
function from sql99 using variables:

select x from (
select x, @a := @a+1 from (
select @a := 0, @b := (select count(1) from T), x
from T
order by rand()
) as V
where @a < 0.3*@b
) as U;

/Lennart

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.