0

以下のようなSQLクエリがあります

select name, loc, status, proj, nbr, sum(sub) as sub, nvl2(val, ''Y'', ''N'') where name = "value" group by name, loc, status, proj, nbr, nvl2(val, ''Y'', ''N'')

基準を使って同じことを書いてみました

ProjectionList proList = Projections.projectionList();
proList.add(Projections.sum("sub"));
criteria.add(Restrictions.like("name", StringUtils.appendWildCard("value")).ignoreCase());              
criteria.addOrder(Order.asc("value"));      

nvl の合体の書き方と、行の順序が正しいかどうかを知りたいです。リードはありますか?

4

1 に答える 1

1

Projections.sqlGroupProjection(...)以下の基準とともに を使用する

    List<CoalesceDemo> list = session.createCriteria(CoalesceDemo.class).
        setProjection(Projections.projectionList()
                .add(Projections.sum("sub"))
                .add(Projections.sqlGroupProjection("nvl2(val, 'Y', 'N') as decodedVal", "nvl2(val, 'Y', 'N')", 
new String [] {"decodedVal"}, new Type[]{BooleanType.INSTANCE}))
                .add(Projections.groupProperty("name"))
                .add(Projections.groupProperty("val"))
                .add(Projections.groupProperty("nbr"))
                .add(Projections.groupProperty("proj"))
                .add(Projections.groupProperty("loc")))
        .add(Restrictions.eq("name", "value")).list();

あなたが探している次のクエリになりました:

select this_.name as y0_, this_.loc as y1_, this_.nbr as y2_, this_.proj as y3_, sum(this_.sub) as y4_, 
nvl2(val, 'Y', 'N') as decodedVal, this_.name as y6_, this_.val as y7_, this_.nbr as y8_, 
this_.proj as y9_, this_.loc as y10_ from CoalesceTable this_ where this_.name=? 
group by nvl2(val, 'Y', 'N'), this_.name, this_.val, this_.nbr, this_.proj, this_.loc

もう 1 つの方法は@Formula、エンティティの新しいフィールドで注釈を使用することです。

私の場合、このフィールドをCoalesceDemoクラスに追加しました

@Formula("nvl2(val, 'Y', 'N')")
public String decodedVal;

そして、以下のように Criteria クエリでこれを使用します。

List<CoalesceDemo> list = session.createCriteria(CoalesceDemo.class).
    setProjection(Projections.projectionList()
            .add(Projections.sum("sub"))
            .add(Projections.groupProperty("name"))
            .add(Projections.groupProperty("val"))
            .add(Projections.groupProperty("nbr"))
            .add(Projections.groupProperty("proj"))
            .add(Projections.groupProperty("decodedVal"))
            .add(Projections.groupProperty("loc")))
    .add(Restrictions.eq("name", "value")).list();

これにより、次のようなクエリが生成されます。

select this_.name as y0_, this_.loc as y1_, this_.nbr as y2_, this_.proj as y3_, sum(this_.sub) as y4_, 
this_.name as y5_, this_.val as y6_, this_.nbr as y7_, this_.proj as y8_, 
nvl2(this_.val, 'Y', 'N') as y9_, this_.loc as y10_ from CoalesceTable this_ where this_.name=? 
group by this_.name, this_.val, this_.nbr, this_.proj, nvl2(this_.val, 'Y', 'N'), this_.loc

これが役立つかどうかを確認し、データに対してテストすることをお勧めします。

于 2016-01-21T12:15:34.393 に答える