How to use Oracle's SAMPLE() and ROWNUM with QueryDSL-SQL?

329 views
Skip to first unread message

Максим Смоляков

unread,
Apr 21, 2017, 5:51:04 PM4/21/17
to Querydsl
Hi all!

My Oracle query retrieves 5 random rows from large dataset:
SELECT users.userid
  FROM users SAMPLE
(1)
 WHERE ROWNUM
<= 5

And I want to implement this case with QueryDsl something like this:
query()
       
.select(users.userid)
       
.from(users)
       
.fetch();

Does QueryDsl supports Oracle's SAMPLE() and ROWNUM syntax?

Himanshu Upadhyay

unread,
Jul 22, 2020, 2:58:06 PM7/22/20
to Querydsl
Hi, I am trying to implement the same using QueryDSL, din't find any alternates,let me know if you. found any. solution. for it.

Jan-Willem Gmelig Meyling

unread,
Jan 27, 2021, 7:54:33 PM1/27/21
to Querydsl
For the `SAMPLE(1)` expression you can use a JoinFlag with a TemplateExpression (Expressions.stringTemplate(...) for example).

For the row num you can use a plain template expression: .where(Expressions.numberTemplate(Integer.class, "ROWNUM").lte(5))`

Op woensdag 22 juli 2020 om 20:58:06 UTC+2 schreef upadhyay...@gmail.com:
Reply all
Reply to author
Forward
0 new messages