1

I have database query similar to below one, it is working fine when i execute this in mySql but it failing in Junit with error "expression not in aggregate or GROUP BY columns:". My JUnit uses in memory HSQL DB. I have gone through the Strange behavior of HSQLDB and group by and understand we need to give group by for all fields when aggregate method is used in the query.

But i have a requirement where i need to get the all the value based on grouping with only one column(which is not primary key), can you please suggest how can i achieve this in JUnit.

Query which I'm executing : Select *, count(sampleField) from TestTable where sampleField2 != null group by sampleField

3
  • be careful... how are you connecting to the DB? because credentials via JNDI will not be accessable through JUnit... the test will need its own hardcoded credentials. speaks from experience Commented Aug 9, 2017 at 16:06
  • With group by you can only use grouped columns and aggregates in the selectlist. don't know how this can work in MySQL except for a table with just one column. Commented Aug 9, 2017 at 16:23
  • There is nothing strange about that, it is simply invalid SQL. It seems you've tried to deduce your question down with some psuedo SQL. Please post the full query that worked in MySQL, along with the complete query you're attempting to execute in HQL. Commented Aug 9, 2017 at 18:40

1 Answer 1

1

You can use min(column_name) or max(column_name) for the other columns.

For example, if you have columns named firstname and lastname

Select min(firstname), min(lastname), count(sampleField) from TestTable where sampleField2 is not null group by sampleField

Edited: use is not null instead of != null for correct results.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.