ORACLE - 意外令牌:(靠近第1行,第95列

时间:2017-11-02 06:59:20

标签: oracle spring-boot

下面是我的Oracle SQL查询,根据状态(DB中的列''OVRL_MET_ST')查找过去12个月的计数(通过或失败),其中CSL_ENHANCE_CHANGES是ORACLE DB中的表,其中列为SRL_NO_TXT作为id(主键) ),日期数据类型的MNTH_TXT(例如“01-APR-17”),OVRL_MET_ST(varchar数据类型)由状态(即“通过”或“失败”)组成

select months.month , 
       cec.ovrlMetSt as Status , 
       coalesce(count(cec.srlNoTxt), 0) as Count 
from (select to_char( trunc(sysdate) - numtoyminterval(level - 1, 'month'), 'mon-yy') as month 
      from dual connect by level <= 12) months 
LEFT outer join CSL_ENHANCE_CHANGES cec 
    on to_char(months.month) = to_char( trunc(mnthTxt), 'mon-yy') 
group by months.month,cec.ovrlMetSt;

这是执行Oracle查询的结果

Month   Status  Count
feb-17  Pass        1
dec-16  null        0
oct-17  null        0
may-17  Pass        1
jul-17  null        0
sep-17  null        0
aug-17  null        0
apr-17  Pass        2
jun-17  null        0
jan-17  Pass        1
nov-17  null        0
mar-17  Pass        1
apr-17  Fail        1

我在我的代码中使用hql查询,如下所示

@PersistenceContext
EntityManager em;

List<Object[]> namedlist= (List<Object[]>) em.createQuery(
                        "select months.month, cec.ovrlMetSt as Status , coalesce(count(cec.srlNoTxt), 0) as Count from (select to_char( trunc(sysdate) - numtoyminterval(level - 1, 'month'), 'mon-yy') as month from dual connect by level <= 12) months LEFT outer join CSL_ENHANCE_CHANGES cec on to_char(months.month) = to_char( trunc(mnthTxt), 'mon-yy') group by months.month,cec.ovrlMetSt"
                        ).getResultList();

在谷歌上搜索我发现,HQL不支持from子句中的子查询。那么,更改查询是否可行,如果是,查询将是什么?如果没有,是否还有其他Spring-Boot Framework可以处理的方式

1 个答案:

答案 0 :(得分:0)

StringBuffer buffer = new StringBuffer();
        buffer.append(" select  cec.OVRL_MET_ST as Status ,to_date(months.month, 'mon-yy') as MONTH_TXT ,  ");
        buffer.append(" coalesce(count(cec.SRL_NO_TXT), 0) as Count from  ");
        buffer.append(
                " (select to_char( trunc(sysdate-3) - numtoyminterval(level - 1, 'month'), 'mon-yy') as month from dual connect by level <= 12) months ");
        buffer.append(" LEFT outer join CSL_ENHANCE_CHANGES cec on ");
        buffer.append("  to_char(months.month) = to_char( trunc(MNTH_TXT), 'mon-yy') ");
        buffer.append("  group by months.month,cec.OVRL_MET_ST ");
        query = buffer.toString();

        Class.forName("oracle.jdbc.driver.OracleDriver");

        PreparedStatement preparedStatement = dbConnection.prepareStatement(query);
        ResultSet reset = preparedStatement.executeQuery();