我有以下 SQL 查询来按一天中的订单日期和小时对订单进行分组:
select to_char(o.order_date, 'YYYY-MM-DD HH24') order_date_hour,
sum(o.quantity) quantity
from orders o
where o.order_date >= to_date('01.02.2016', 'DD.MM.YYYY')
and o.order_date < to_date('03.02.2016', 'DD.MM.YYYY')
group by to_char(o.order_date, 'YYYY-MM-DD HH24')
order by to_char(o.order_date, 'YYYY-MM-DD HH24');
结果示例如下:
ORDER_DATE_HOUR | QUANTITY
2016-02-01 06 | 10
2016-02-03 09 | 20
该查询使用 SQL 开发人员按预期工作。在 QueryDSL 中,我提出了以下查询:
SQLQuery q = queryFactory.createSQLQuery();
q.from(order);
q.where(order.orderDate.goe(Timestamp.valueOf(from)))
.where(order.orderDate.lt(Timestamp.valueOf(to)));
q.groupBy(to_char(order.orderDate, "YYYY-MM-DD HH24"));
q.orderBy(order.orderDate.asc());
List<Tuple> result = q.list(to_char(order.orderDate, "YYYY-MM-DD HH24"), order.quantity);
to_char 是我在此线程中找到的一种方法:https ://groups.google.com/forum/#!msg/querydsl/WD04ZRon-88/nP5QhqhwCUcJ
我得到的例外是:
java.sql.SQLSyntaxErrorException: ORA-00979: 不是 GROUP BY 表达式
我尝试了一些查询的变体,但一点运气都没有。
有谁知道为什么查询失败?谢谢 :)