我使用连接同一个表(self join)和having count但结果并不像预期的那样我正在使用oracle数据库

xcitsw88  于 2021-08-09  发布在  Java
关注(0)|答案(2)|浏览(320)

我试着运行下面的查询。我希望rp\ num列中的所有数据都在rp\ num\列组中。我还希望列rp\u num的所有数据都在列std\u id\u组中,该列的计数不止一个(std\u id,rp\u fam,rn,rp\u id中有多个计数)。但是通过我的查询,我得到的数据在rp\u num\u group和std\u id\u group列中的计数都大于1。请建议正确的方法来使用连接以获得正确的结果。
例如,如果我的表如下所示:

std_id      rn      rp_id       rp_fam      rp_num

1234        1234    1           abc         a12
1234        1235    1           abc         a34
1234        1236    1           xyz         a56
1244        1246    2           xyz         234
1244        1245    2           hgf         z65
1254        1456    2           hgf         z78

我希望我的结果是:

std_id      rp_num_group        std_id_group
1234        a12 a34 a56          a12:abc  a34:abc
1244        234 265 z78          z65:hgf  z78:hgf

但我的结果如下:

std_id      rp_num_group        std_id_group
1234        a12 a34            a12:abc  a34:abc
1244        z65 z78            z65:hgf  z78:hgf
SELECT DISTINCT rrpn.std_id,  
                          Stringagg(rrpn.rp_num) 
                          over ( 
                            PARTITION BY tmta.rp_id,tmta.rn,tmta.std_id
                            ORDER BY Nlssort(tmta.mta, 'NLS_SORT=BINARY_CI') ROWS 
                          BETWEEN unbounded 
                          preceding AND unbounded following ) AS rp_num_group,
                            Stringagg_distinct(rrpn3.rp_num||':'||rrpn3.rp_fam ) 
                          over ( 
                            PARTITION BY rrpn3.std_id, rrpn3.rn, rrpn3.rp_id 
                            ORDER BY Nlssort(rrpn3.rp_num, 'NLS_SORT=BINARY_CI') ROWS 
                          BETWEEN unbounded 
                          preceding AND unbounded following ) AS std_id_group
        FROM   data_rrpn rrpn 
                join data_rrpn rrpn3
                  ON rrpn3.std_id = rrpn.std_id 
                AND rrpn3.rp_fam = rrpn.rp_fam 
                AND rrpn3.rn = rrpn.rn 
                AND rrpn3.rp_id = rrpn.rp_id 
               join (SELECT std_id, 
                            rp_fam, 
                            rn, 
                            rp_id, 
                            Count(*) 
                     FROM   data_rrpn 
                     GROUP  BY std_id, 
                               rp_fam, 
                               rn, 
                               rp_id 
                     HAVING Count(*) > 1) rrpn2 
                 ON rrpn3.std_id = rrpn2.std_id 
                    AND rrpn3.rp_fam = rrpn2.rp_fam 
                    AND rrpn3.rn = rrpn2.rn 
                    AND rrpn3.rp_id = rrpn2.rp_id 
        ORDER  BY rrpn.std_id;
bbmckpt7

bbmckpt71#

似乎您需要简单的字符串聚合,对具有多个记录的组进行筛选:

select 
    std_id,
    listagg(rp_num, ' ') within group(order by rp_num) rp_num_group,
    listagg(rp_num || ':' || rp_fam, ' ') within group(order by rp_num) std_id_group
from data_rrpn 
group by std_id
having count(*) > 1
wmtdaxz3

wmtdaxz32#

你很可能只想在 std_id, rp_fam, rp_id 而不是开着 RN (哪个是独一无二的?)。
在这里你可以预览一下 rp_num 将考虑( GRP_CNT > 1 ):

select a.*,
count(*) over (partition by std_id, rp_fam, rp_id) grp_cnt
from tst a;

    STD_ID         RN      RP_ID RP_FA RP_NU    GRP_CNT
---------- ---------- ---------- ----- ----- ----------
      1234       1234          1 abc   a12            2
      1234       1235          1 abc   a34            2
      1234       1236          1 xyz   a56            1
      1244       1245          2 hgf   z65            1
      1244       1246          2 xyz   234            1
      1254       1456          2 hgf   z78            1

最后一个问题是

with grp as
(
select a.*,
count(*) over (partition by std_id, rp_fam, rp_id) grp_cnt
from tst a
)
select STD_ID,
  listagg(RP_NUM, ' ') within group(order by RP_NUM) rp_num_group,
  listagg(case when grp_cnt > 1 then RP_NUM || ':' || RP_FAM end, ' ') within group(order by RP_NUM) std_id_group
from grp
group by STD_ID;

给出结果:

STD_ID, RP_NUM_GROUP, STD_ID_GROUP
1234    a12 a34 a56   a12:abc a34:abc
1244    234 z65 
1254    z78

不是因为你得到了 z65:hgf z78:hgf 在您的示例结果中,但是我认为如果有一些额外的魔术逻辑,您只需要调整 GRP_CNT 使用上述分析函数。

相关问题