[Solved] RAND() - Unexpected results

Discuss the database features
Post Reply
arfgh
Posts: 566
Joined: Tue Mar 05, 2013 6:44 pm

[Solved] RAND() - Unexpected results

Post by arfgh »

i did the follow query:
SELECT "ID", "Title" FROM "Movies" WHERE "ID" = ROUND( ( RAND( ) * ( SELECT MAX( "ID" ) FROM "Movies" ) ), 0 )
sometimes there is no result, and sometimes there is a single result in a row, and sometimes there are 2 or 3 results.
Only one random number and one result expected.

How is possible ? did i something bad on the query ? ... the most probable.
Last edited by arfgh on Thu Nov 10, 2016 10:41 pm, edited 1 time in total.
OpenOffice last version | Mageia Linux x64 | Ubuntu Linux | Windows 8.1 Enterprise x64 | Java last version
chrisb
Posts: 298
Joined: Mon Jun 07, 2010 4:16 pm

Re: RAND() - Unexected results

Post by chrisb »

the value returned by your where clause will vary with each row processed (due to rand()).
try this:-

Code: Select all

select ID, "Title"
from
"Movies"

join
(select round(rand() * max(ID), 0) ID from "Movies") "Rand_ID"
on "Rand_ID".ID = "Movies".ID
open office 4.1.14 & LibreOffice 6.4.4.2 x64 using HSQL 1.8.0.10 (Embedded) and HSQL 2.6.0 (Split) on Windows 10
arfgh
Posts: 566
Joined: Tue Mar 05, 2013 6:44 pm

Re: RAND() - Unexected results

Post by arfgh »

yes but if i obtain a single number from rand operation, why to get, sometimes, no results, other times 1, other times 2...... i cant see it
OpenOffice last version | Mageia Linux x64 | Ubuntu Linux | Windows 8.1 Enterprise x64 | Java last version
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: RAND() - Unexected results

Post by Villeroy »

SELECT TOP 1 FROM "Movies" ORDER BY RAND()
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
arfgh
Posts: 566
Joined: Tue Mar 05, 2013 6:44 pm

Re: RAND() - Unexected results

Post by arfgh »

villery i didnt understand that query you suggested....

anyways now i understood what chrisb pointed. Each comparison in my query threw a different rand value, for that reason failed or showed unexpected results.
So the way to fix it was to perform a single random value and join it to the main query... i guess....
OpenOffice last version | Mageia Linux x64 | Ubuntu Linux | Windows 8.1 Enterprise x64 | Java last version
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: RAND() - Unexected results

Post by Villeroy »

It selects a random record from "Movies".
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
arfgh
Posts: 566
Joined: Tue Mar 05, 2013 6:44 pm

Re: RAND() - Unexected results

Post by arfgh »

ok ok, only works in direct mode
interesting way to do it, anyways

Code: Select all

select top 1 "ID", "Title" from "Movies" order by RAND()
OpenOffice last version | Mageia Linux x64 | Ubuntu Linux | Windows 8.1 Enterprise x64 | Java last version
User avatar
Sliderule
Volunteer
Posts: 1279
Joined: Thu Nov 29, 2007 9:46 am

Re: RAND() - Unexected results

Post by Sliderule »

arfgh wrote:ok ok, only works in direct mode
interesting way to do it, anyways

Code: Select all

select top 1 "ID", "Title" from "Movies" order by RAND()
If you insist on running the Query while using the Base Parser ( NOT Direct Mode ) . . . use this syntax instead:

Code: Select all

Select 
   "ID", 
   "Title" 
From "Movies" 
Order By RAND() 
LIMIT 1
Thanks to add [Solved] in your 1st post Subject (edit button top right) if this issue has been resolved.
arfgh
Posts: 566
Joined: Tue Mar 05, 2013 6:44 pm

Re: Solved - RAND() - Unexected results

Post by arfgh »

thanx guys, all of them seems very interesting ways.
OpenOffice last version | Mageia Linux x64 | Ubuntu Linux | Windows 8.1 Enterprise x64 | Java last version
Post Reply