我的查询非常漫长而复杂,我正在使用spring数据,
有人可以帮助我在spring jpa中实现以下查询吗,因为spring不支持子查询中的参数注入,所以我不能使用本机查询。
select desc as desc, priority as priority, activeUser as activeUser,
totalUser as totalUser
from
(
select stat.desc as desc,
stat.priority as priority,
(case when sum(activeUser) is null then 0 else sum(activeUser) end) as
activeUser,
(case when sum(totalUser) is null then 0 else sum(totalUser) end) as
totalUser
from lookup.user stat left outer join
(
select user.role as role,
sum (case when user.STATUS = 'ACTIVE' then 1 else 0 end) as activeUser,
count(*) as totalUser,
user.group as group
from ctrl.user user
where user.group =:userGroup
and user.branch_code =:branchCode
group by user.role,user.group
) as tbl on stat.role = tbl.role and stat.group = tbl.group
where stat.group =:userGroup
group by stat.desc, stat.priority
union
select 'Total' as desc,
'999' as priority,
(case when sum(activeUser) is null then 0 else sum(activeUser) end) as
activeUser,
(case wh en sum(totalUser) is null then 0 else sum(totalUser) end) as
totalUser
from lookup.user stat left outer join
(
select user.role as role,
sum (case when user.STATUS = 'ACTIVE' then 1 else 0 end) as activeUser,
count(*) as totalUser,
user.group as group
from ctrl.user user
where user.group =:userGroup
and user.branch_code =:branchCode
group by user.role,user.group
) as tbl on stat.role = tbl.role and stat.group = tbl.group
where stat.group =:userGroup
) as tblMain order by priority
我尝试按如下方式生成predicate
,但它不占用selects
Specification<CtrlUser> sp= new Specification<CtrlUser>() {
@Override
public Predicate toPredicate(Root<CtrlUser> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
// TODO Auto-generated method stub
query.multiselect(root.get("role"), cb.selectCase().when(root.get("status").in("ACTIVE"), 1).otherwise(0).alias("ActiveUsers"),
cb.count(root).alias("totalUsers"),root.get("group")).groupBy(root.get("role"),root.get("group"));
return cb.and(cb.equal(root.get("branchCode"), branchCode), cb.equal(root.get("group"), userGroup));
}
};
我在网上搜索过,但是运气不高。
如果需要更多信息,请告诉我。