valueCollectionDatasource query using "CASE WHEN..."

For a valueCollectionDatasource, I am trying to sum some payments if they match a paymentType condition.
I would like to show the sum for each paymentType in a separate column, so I was thinking of using “CASE WHEN” in my query, as follows:

SELECT pmt.payDay,,
SUM( CASE WHEN pmt.paymentType = 0 THEN pmt.amount ELSE 0 END),
SUM( CASE WHEN pmt.paymentType = 1 THEN pmt.amount ELSE 0 END),
SUM( CASE WHEN pmt.paymentType = 2 THEN pmt.amount ELSE 0 END),
SUM( CASE WHEN pmt.paymentType = 3 THEN pmt.amount ELSE 0 END),
FROM tojm$Payment pmt
WHERE = :ds$concertDs
GROUP BY pmt.payDay,

This however results in JpqlSyntaxException: Errors found for input jpql.
The error is at “CASE WHEN”…

Can I use this kind of query in Cuba?
Thank you for your help,

Hi Matthis,

“CASE WHEN” is not implemented in CUBA yet because we use a parser that conforms to the JPA 2.1 spec which states that aggregate function applied to a path expression. But we’ll consider supporting it in the future because EclipseLink can execute such queries.

Hi Andrey,
Thanks for your answer - understood that this is not supported currently.
Would an alternative using a native sql query to set the valueCollectionDatasource source be possible?


what i do in these cases oftentimes is just to create a SQL VIEW that will enhance the data like you did with your case. So you basically split the query into the definition of a view and the normal “WHERE” part. Additionally you create an Entity for the View that will just take the VIEW as the table name.

You’ll end up with something like this:

SELECT pmtView.payDay,,
FROM tojm$PaymentSumView pmtView
WHERE = :ds$concertDs

And the logic just shifts into the view creation. In this case you are not constraint by the JPQL query anymore, because you write your create statements for the tables & views db-specific anyway (in 10-create-db.sql e.g.)…


Hi Mario,

Thank you very much for your interesting reply, and apologies for the late reply.
I will try this approach!

:ticket: See the following issue in our bug tracker: