如何使用弹簧数据进行分组 [英] how to groupBY using spring data

查看:151
本文介绍了如何使用弹簧数据进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在我的项目中使用了spring数据,我试着按两个字段分组,并请求:

  @Query(来自Agence obj的SELECT obj GROUP BY obj.secteur.nomSecteur,obj.nomAgence)
Iterable< Agence> getSecteurAgenceByPc();

但它不适用于我..我想要的是这样的结果:

  -Safi 
-CTM
CZC1448YZN
2UA13817KT

- 加元
-CTM
CZC1349G1B
2UA0490SVR
-Agdal
G3M4NOJ

-Essaouira
-CTM
CZC1221B85
- 加油Routiere Municipale
CZC145YL3

我得到的是

  {
status:0,
data:
[
{
secteur :萨菲,
agence:CTM
},
{
secteur:Safi,
agence:Dep
},
{
secteur:Rabat,
agence:Agdal
},
{
secteur:Rabat,
agence:CTM
},
{
secteur:Essaouira,
agence: CTM
},
{
secteur:Essaouira,
agence:Gare Routiere Municipale
}
]
}


解决方案

JPQL无法实现。



Group By 做什么?



它将group by子句中列中相同的所有行组合到一行中。由于它将多行合并为一行,因此其他列中的数据只能以某种组合方式存在。例如,您可以包含MIN / MAX或AVG值,但不包括原始值。



此外,结果始终是一张表,而不是一棵树。



另外注意:没有重复数据。每一个 secteur agence 的组合都只出现一次。如果你想要一个树结构,你必须写一些java的代码。


hi i'm using spring data in My project and I'm trying group by two fields, heres the request:

@Query( "SELECT obj from Agence obj GROUP BY obj.secteur.nomSecteur,obj.nomAgence" )
  Iterable<Agence> getSecteurAgenceByPc();

but it doesnt work for me..what i want is this result:

-Safi
  -CTM
    CZC1448YZN
    2UA13817KT

-Rabat
  -CTM
    CZC1349G1B
    2UA0490SVR
  -Agdal
    G3M4NOJ

-Essaouira
  -CTM
    CZC1221B85
  -Gare Routiere Municipale
    CZC145YL3

What I get is

{
    "status": 0,
    "data":
    [
        {
            "secteur": "Safi",
            "agence": "CTM"
        },
        {
            "secteur": "Safi",
            "agence": "Dep"
        },
        {
            "secteur": "Rabat",
            "agence": "Agdal"
        },
        {
            "secteur": "Rabat",
            "agence": "CTM"
        },
        {
            "secteur": "Essaouira",
            "agence": "CTM"
        },
        {
            "secteur": "Essaouira",
            "agence": "Gare Routiere Municipale"
        }
    ]
}

解决方案

What you want is not possible with JPQL.

What does Group By do?

It combines all rows that are identical in the columns in the group by clause in to one row. Since it combines multiple rows into one, data in other columns can only be present in some combined fashion. For example, you can include MIN/MAX or AVG values, but never the orginal values.

Also the result with always be a table, never a tree.

Also note: there is no duplicated data. Every combination of secteur and agence appears exactly once.

If you want a tree structure, you have to write some java code for that.

这篇关于如何使用弹簧数据进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆