使用带有CASE WHEN的聚合函数时在JDO查询中获取异常

时间:2014-12-24 05:30:53

标签: google-app-engine google-cloud-datastore jdo datanucleus

我在我的gae中执行以下查询并获得异常。

    PersistenceManager pm = getPersistenceManager();

    StringBuilder sb = new StringBuilder();
    sb.append(" select date, vehicleCode, vehicleSubCode, colorCode, ");
    sb.append(" SUM(CASE WHEN transactionType = 0 THEN count ELSE 0 END) stock, ");
    sb.append(" SUM(CASE WHEN transactionType = 1 THEN count ELSE 0 END) sale, ");
    sb.append(" SUM(CASE WHEN transactionType = 2 THEN count ELSE 0 END) transfer ");
    sb.append(" from Stock as AggregatedStock ");

    Query query = pm
            .newQuery(sb.toString());

    query.setFilter(" updatedByDealer == " + dealer);
    query.setGrouping(" date, vehicleCode, vehicleSubCode, colorCode ");
    query.setOrdering(" vehicleCode desc ");

    query.declareImports("import com.sandeepapplabs.dms.Stock");

我得到的例外是

com.google.api.server.spi.SystemService invokeServiceMethod: ')' expected at character 57 in            "date, vehicleCode, vehicleSubCode, colorCode,  SUM(CASE WHEN transactionType = 0 THEN count ELSE 0 END) -  SUM(CASE WHEN transactionType = 1 THEN count ELSE 0 END) -  SUM(CASE WHEN transactionType = 2 THEN count ELSE 0 END)"
javax.jdo.JDOUserException: ')' expected at character 57 in "date, vehicleCode, vehicleSubCode, colorCode,  SUM(CASE WHEN transactionType = 0 THEN count ELSE 0 END) -  SUM(CASE WHEN transactionType = 1 THEN count ELSE 0 END) -  SUM(CASE WHEN transactionType = 2 THEN count ELSE 0 END)"
at org.datanucleus.api.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:519)
at org.datanucleus.api.jdo.JDOQuery.execute(JDOQuery.java:230)
at com.sandeepapplabs.dms.AggregatedStockEndpoint.listAggregatedStock(AggregatedStockEndpoint.java:56)

类似的查询在SQLFiddle

中工作正常

SQLFiddle example

0 个答案:

没有答案