Question

InlineQuerier SQL random statement ("Random()", "Rand()", or "RND()"?) in conjunction with "LIMIT"

  • 30 November 2017
  • 4 replies
  • 2 views

Badge

Hello All,

Question regarding the InlineQuerier transformer that harnesses SQL. I'm trying to select a random subset of records from a table. Here's the SQL code in MS-Access that works (different syntax).

SELECT TOP 200 *
FROM (SELECT Rnd(ID) AS RandomID, * FROM TableNameHere)  AS TableNameRandomSelection
ORDER BY RandomID;

I think the InlineQuerier uses SQLite...correct me if I'm wrong. I'm uncertain as to which random function I should use and where to implement it within the code string.

RND()? RANDOM()? RAND()?

And can I add an argument to this function? I need to generate a random sample of records based off the table's primary key ("ID" in this case).

This code works, but it only returns the first 200 records in the table (not the random sample I'm looking for), but I feel like I'm close and just need to work a random function in there...

SELECT *
FROM TableName
LIMIT 200

4 replies

Userlevel 1
Badge +21
SELECT "ID" FROM "Table" WHERE "ID" IN (SELECT "ID" FROM "Table" ORDER BY RANDOM() LIMIT 200)

Something like the above should work

Badge

A reply to my own post....Just FYI.

This works...

SELECT * FROM TableNameHere
WHERE ID IN (SELECT ID FROM TableNameHere ORDER BY RANDOM() LIMIT 200)

Badge
SELECT "ID" FROM "Table" WHERE "ID" IN (SELECT "ID" FROM "Table" ORDER BY RANDOM() LIMIT 200)

Something like the above should work

Thanks egomm. Beat me to it :)

 

 

Userlevel 2
Badge +17

A reply to my own post....Just FYI.

This works...

SELECT * FROM TableNameHere
WHERE ID IN (SELECT ID FROM TableNameHere ORDER BY RANDOM() LIMIT 200)

 

This query could work as well.

 

select * from tablename order by random() limit 200

Reply