2016-04-11 26 views
3

Ben bir günde emir tarih ve saate göre grup emirlerine aşağıdaki SQL sorgusu:QueryDSL grup

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'); 

aşağıdaki sonucu için bir örnek olarak:

ORDER_DATE_HOUR | QUANTITY 
2016-02-01 06 | 10 
2016-02-03 09 | 20 

Sorgu, SQL geliştiricisi kullanılarak beklendiği gibi çalışır. https://groups.google.com/forum/#!msg/querydsl/WD04ZRon-88/nP5QhqhwCUcJ

alıyorum istisnadır:

java.sql.SQLSyntaxErrorException:

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 bu parçacığı bulunan bir yöntemdir: QueryDSL yılında aşağıdaki sorgu ile geldi ORA -00979: GROUP BY ifadesi yok

Hiç şanssız sorguda birkaç varyasyon denedim.

Sorgu neden başarısız olduğunu bilen var mı? Sen özel anlatımlar inşa etmek için StringTemplate ve DateTemplate kullanabilirsiniz

+1

, ama hızlı bir aramadan kodunuzun son satırında 'order.quantity.sum()' olmalıdır? Şu anda bu sütunu birleştirmiyorsunuz, ki bu da düşündüğüm hatayı açıklıyor. –

+0

Bu sorun olabilir. Her şeyi tekrar kontrol ettim ve şimdilik işe yarıyor. Teşekkür ederim :) –

cevap

3

:) sayesinde gibi birim testinde com.querydsl.sql.TemplateTest yapılan: Burada

StringTemplate datePath = Expressions.stringTemplate(
    "to_char({0},'{1s}')", order.orderDate, ConstantImpl.create("YYYY-MM-DD HH24")); 

DateTemplate from = Expressions.dateTemplate(
    Date.class, "to_date({0},'{1s}')", fromStr, ConstantImpl.create("DD.MM.YYYY")); 

DateTemplate to = Expressions.dateTemplate(
    Date.class, "to_date({0},'{1s}')", toStr, ConstantImpl.create("DD.MM.YYYY")); 

query.select(datePath.as("order_date_hour"), order.quantity.sum().as("quantity")) 
.from(order) 
.where(order.orderDate.goe(from) 
    .and(order.orderDate.lt(to))) 
    .groupBy(datePath) 
    .orderBy(datePath.asc()); 

List<Tuple> results = query.fetch(); 

query.getSQL().getSQL() için çıktı: Bu kullanılan Hiç

select to_char("order".order_date,'YYYY-MM-DD HH24') order_date_hour, sum("order".quantity) quantity 
from "order" "order" 
where "order".order_date >= to_date(?,'DD.MM.YYYY') and "order".order_date < to_date(?,'DD.MM.YYYY') 
group by to_char("order".order_date,'YYYY-MM-DD HH24') 
order by to_char("order".order_date,'YYYY-MM-DD HH24') asc