Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jooq nested select

Tags:

java

sql

jooq

First time jooq user here. I need to convert regular SQL statement with nested select below into jooq. Any idea if I am on the right path? I appreciate any help.

//select * 
//from profile 
//where (profile_id, effective_date) in (
//                select profile_id, max(effective_date) as date 
//                from profile 
//                group by profile_id
//                )

This is what I have but not sure if even correct:

Result<Record> profiles = dsl_
.select(PROFILE.fields())
.from(PROFILE)
.where(PROFILE.PROFILE_ID, PROFILE.EFFECTIVE_DATE) in (create
    .select(PROFILE.PROFILE_ID, max(PROFILE.EFFECTIVE_DATE) as date
    .from(PROFILE)
    .groupBy(PROFILE.PROFILE_ID)))
    .fetch();
like image 785
hln98 Avatar asked Feb 11 '16 15:02

hln98


1 Answers

You want to use the DSL.row() constructor, in order to construct a row value expression predicate.

Here's how to do that with jOOQ:

// Assuming this:
import static org.jooq.impl.DSL.*;

// Write
Result<Record> profiles = dsl_
.select(PROFILE.fields())
.from(PROFILE)
.where(row(PROFILE.PROFILE_ID, PROFILE.EFFECTIVE_DATE).in(
     select(PROFILE.PROFILE_ID, max(PROFILE.EFFECTIVE_DATE).as("date"))
    .from(PROFILE)
    .groupBy(PROFILE.PROFILE_ID)
))
.fetch();
like image 102
Lukas Eder Avatar answered Oct 16 '22 03:10

Lukas Eder