我有一个带值的表,(SAMPLE VALUES)
____________________
|DATE FORM |
|2016-01-21 NEW |
|2016-01-21 OLD |
|2016-01-21 NEW |
|2016-01-22 NEW |
|2016-01-22 OLD |
|2016-01-24 OLD |
|2016-01-24 NEW |
|2016-01-25 NEW |
|2016-01-26 OLD |
|____________________|
我如何编写hibernate条件来获取FORM的计数,每个NEW和OLD在每个日期的单独列中显示如下。
________________________
|DATE NEW OLD|
|2016-01-21 2 1 |
|2016-01-22 1 1 |
|2016-01-24 1 1 |
|2016-01-25 1 0 |
|2016-01-26 0 1 |
|________________________|
我目前正在按照此标准获得以下结果。(上表数据为例)
Criteria crit = sessionC.createCriteria(OP_DOCTOR_VISIT.class);
crit.add(Restrictions.ge("USER_DATETIME", fdate));
crit.add(Restrictions.le("USER_DATETIME", tdate));
ProjectionList p = Projections.projectionList();
p.add(Projections.sqlProjection("date(USER_DATETIME) as beginDate", new String[]{"beginDate"}, new Type[]{StandardBasicTypes.DATE}));
p.add(Projections.sqlProjection("date(USER_DATETIME) as endDate", new String[]{"endDate"}, new Type[]{StandardBasicTypes.DATE}));
p.add(Projections.groupProperty("FORM"));
p.add(Projections.count("FORM"));
p.add(Projections.alias(Projections.sqlGroupProjection("date(USER_DATETIME) as createdDate", "createdDate", new String[]{"createdDate"}, new Type[]{StandardBasicTypes.DATE}), "DAT"));
crit.addOrder(Order.asc("DAT"));
crit.setProjection(p);
List l = crit.list();
| 2016-01-25| 2016-01-25| NEW| 2 | 2016-01-25|
| 2016-01-25| 2016-01-25| OLD| 1 | 2016-01-25|
| 2016-01-27| 2016-01-27| NEW| 1 | 2016-01-27|
| 2016-01-27| 2016-01-27| OLD| 1 | 2016-01-27|
| 2016-01-28| 2016-01-28| NEW| 41| 2016-01-28|
| 2016-01-28| 2016-01-28| OLD| 1 | 2016-01-28|
| 2016-01-29| 2016-01-29| NEW| 2 | 2016-01-29|
| 2016-01-29| 2016-01-29| OLD| 10| 2016-01-29|
如何使日期与众不同,并在单独的列中获取NEW和OLD的结果。
我已经使用sql查询,任何人都可以帮助我将其转换为休眠标准或查询。
select count(ODV.FORM) - count(ODV1.OP_VISIT_ID) as 'OLD', count(ODV1.OP_VISIT_ID) as 'NEW' ,
DATE_FORMAT(ODV.USER_DATETIME,'%d-%m-%Y') as 'VISIT_DATE'
from OP_DOCTOR_VISIT ODV
LEFT OUTER JOIN OP_DOCTOR_VISIT ODV1 on ODV.OP_VISIT_ID = ODV1.OP_VISIT_ID
and (ODV1.FORM = 'NEW' and ODV1.USER_DATETIME >='2016-01-25' and ODV1.USER_DATETIME <='2016-01-30')
where (ODV.USER_DATETIME >='2016-01-25' and ODV.USER_DATETIME <='2016-01-30')
group by DATE_FORMAT(ODV.USER_DATETIME,'%d-%m-%Y')
答案 0 :(得分:1)
找到我的问题的答案:
Criteria crit1 = sessionC.createCriteria(OP_DOCTOR_VISIT.class, "OPDV1");
crit.createAlias("OPDV1.OP_VISIT", "OPDV2", JoinType.LEFT_OUTER_JOIN,
Restrictions.and(Restrictions.eq("OPDV2.FORM", "NEW"), Restrictions.ge("OPDV2.USER_DATETIME", fdate), Restrictions.le("OPDV2.USER_DATETIME", tdate)));
crit.add(Restrictions.ge("OPDV1.USER_DATETIME", fdate));
crit.add(Restrictions.le("OPDV1.USER_DATETIME", tdate));
ProjectionList p1 = Projections.projectionList();
p1.add(Projections.alias(Projections.count("OPDV1.OP_VISIT_ID"), "TOTAL"));
p1.add(Projections.count("OPDV2.FORM"));
p1.add(Projections.alias(Projections.sqlGroupProjection("date(this_.USER_DATETIME) as createdDate", "createdDate", new String[]{"createdDate"}, new Type[]{StandardBasicTypes.DATE}), "DAT"));
crit.setProjection(p1);
return crit.list();