Jooq not generate alias to AggregateFunction<T> in MySQL

72 views
Skip to first unread message

mali...@gmail.com

unread,
Mar 27, 2015, 8:06:20 AM3/27/15
to jooq...@googlegroups.com
Hi,

Jooq not generate alias to AggregateFunction<T> in MySQL

Example:
  
     
AggregateFunction<Integer> fieldCount = sum(fieldByName(BigDecimal.class, "measure", measure.getColumnName()).as(
                        measure
.getColumnName()));

...


DSLContext create = using(con, SQLDialect.MYSQL);
create
.select(fieldCount).from(TABLE);


Generate:
  
     SELECT SUM(med_field) from TABLETEST;


Thanks.

Lukas Eder

unread,
Mar 30, 2015, 3:11:07 AM3/30/15
to jooq...@googlegroups.com
I think the output is correct. What you've done essentially is:

- create a column "measure"."med_field"
- alias that column to just "med_field"
- get the sum of that "med_field" column.

What did you really want to do? This?

select sum(measure.med_field) as med_field from tabletest;

Then you should inverse the call order:

AggregateFunction<Integer> fieldCount = sum(fieldByName(BigDecimal.class, "measure", measure.getColumnName())).as(
                        measure
.getColumnName());

... instead of (check out parentheses):

AggregateFunction<Integer> fieldCount = sum(fieldByName(BigDecimal.class, "measure", measure.getColumnName()).as(
                        measure
.getColumnName()));

Hope this helps,
Lukas

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mali...@gmail.com

unread,
Mar 30, 2015, 7:54:04 AM3/30/15
to jooq...@googlegroups.com, mali...@gmail.com
Hi.. thanks..

But if I use:

AggregateFunction<Integer> fieldCount = sum(fieldByName(BigDecimal.class, "measure", measure.getColumnName())).as(
                        measure
.getColumnName());

I have this error:

Type mismatch: cannot convert from Field<Integer> to AggregateFunction<Integer>  or

if i use AggregateFunction<Integer> fieldCount = (AggregateFunction<Integer>) count(fieldByName(BigDecimal.class, "measure",
                        measure.getColumnName())).as(measure.getColumnName());

In runtime, i have:  org.jooq.impl.FieldAlias cannot be cast to org.jooq.AggregateFunction

Lukas Eder

unread,
Mar 30, 2015, 12:10:34 PM3/30/15
to jooq...@googlegroups.com
Hello,

I'm sorry, I overlooked that bit. Yes, a alias column expression is no longer an aggregate function, so you'll need to assign it to Field<Integer>. You hardly ever need to keep references of type AggregateFunction<T>, unless you want to continue calling DSL methods on it, that transform the aggregate function into a window function, for instance...

I'm not sure if this makes sense to you...?

Cheers
Lukas

--

mali...@gmail.com

unread,
Mar 30, 2015, 1:02:55 PM3/30/15
to jooq...@googlegroups.com, mali...@gmail.com
Thank you. I will use with Field<?>.

Ricardo Malikoski


Em sexta-feira, 27 de março de 2015 09:06:20 UTC-3, mali...@gmail.com escreveu:
Reply all
Reply to author
Forward
0 new messages