我试着运行下面的查询。我希望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;
2条答案
按热度按时间bbmckpt71#
似乎您需要简单的字符串聚合,对具有多个记录的组进行筛选:
wmtdaxz32#
你很可能只想在
std_id, rp_fam, rp_id
而不是开着RN
(哪个是独一无二的?)。在这里你可以预览一下
rp_num
将考虑(GRP_CNT > 1
):最后一个问题是
给出结果:
不是因为你得到了
z65:hgf z78:hgf
在您的示例结果中,但是我认为如果有一些额外的魔术逻辑,您只需要调整GRP_CNT
使用上述分析函数。