Gathering the first experience mit jooq's multiset I try to figure out how to order the result set based on some criteria for the multiset. Consider a datastructure with a Product Table and each product can be assigned an arbitrary amount of storages.
When fetching products with this query:
List<ProductFilterItem> items =
dslContext
.select(
PRODUCT.ID,
PRODUCT.NAME
multiset(
select(
PRODUCT_STORAGE.ID,
PRODUCT_STORAGE.PRODUCT_ID,
PRODUCT_STORAGE.STOCK
PRODUCT_STORAGE.MIN_STOCK
)
.from(PRODUCT_STORAGE)
.where(PRODUCT_STORAGE.PRODUCT_ID.eq(PRODUCT.ID))
).as("storage").convertFrom(r -> r.into(ProductStorageItem.class))
)
.from(PRODUCT)
.where(queryCondition)
.fetchInto(ProductItem.class)
how to order the results based on criteria for the multiset.
E.g put those first that have a storages assigned.
orderBy(inline(3).desc())
gives the desired result, but to me it is not clear what the actual sort comparison is doing.
How to achieve more complex sorting, e.g. order by
min(
(PRODUCT_STORAGE.STOCK - PRODUCT_STORAGE.MIN_STOCK)
.divide(PRODUCT_STORAGE.MIN_STOCK.multiply(0.01))
)
or other computations based on stock values.
Thanks for your help!
Kind Regards, Andreas