Use Alias Name in Group By

514 views
Skip to first unread message

pradeep reddy

unread,
Apr 16, 2015, 7:27:49 AM4/16/15
to quer...@googlegroups.com
Hi,

i have following query 

select case when name like '%internal%' then "permanent" else 
 'temporary' end as name_alias ,sum(sal)
from emp 
group by name_alias;

where i am using alias name in group by which is working fine in MYSQL .

I tried to form this query using Querydsl but not able to.

could please suggest me a way to create it if Querydsl supports this kind of queries.

Thanks in advance


 


timowest

unread,
Apr 16, 2015, 12:42:33 PM4/16/15
to quer...@googlegroups.com
Something like this should work

StringPath nameAlias = Expressions.stringPath("name_alias");
List<String> results = query.from(emp).groupBy(nameAlias)
    .list(new CaseBuilder().when(emp.name.like("%Internal%")).then("permanent").otherwise("temporary"));

pradeep reddy

unread,
Apr 17, 2015, 3:53:20 AM4/17/15
to quer...@googlegroups.com

what is name_alias ???

I want the case expression alias name to be used in group by.

Timo Westkämper

unread,
Apr 17, 2015, 4:04:25 AM4/17/15
to Querydsl on behalf of pradeep reddy
Update

StringPath nameAlias = Expressions.stringPath("name_alias");
List<Tuple> results = query.from(emp).groupBy(nameAlias)
    .list(new CaseBuilder().when(emp.name.like("%Internal%")).then("permanent").otherwise("temporary").as(nameAlias), emp.sal.sum());

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

Reply all
Reply to author
Forward
0 new messages