Java 选择行的所有字段,并根据一列对它们进行分组
数据库中的表有a、b、c列。数据库中的每两行在c列中有相同的值。我想为下一步的操作获取并存储这些对。 我使用的是hibernate,但不是criteria接口。 最好的解决方案是什么 个人和实体:Java 选择行的所有字段,并根据一列对它们进行分组,java,sql,oracle,hibernate,Java,Sql,Oracle,Hibernate,数据库中的表有a、b、c列。数据库中的每两行在c列中有相同的值。我想为下一步的操作获取并存储这些对。 我使用的是hibernate,但不是criteria接口。 最好的解决方案是什么 个人和实体: +--------------+----+-------+---+ | person Object| a |b | c | +--------------+----+-------+---+ | p1 | w | d | 1 | | p2
+--------------+----+-------+---+
| person Object| a |b | c |
+--------------+----+-------+---+
| p1 | w | d | 1 |
| p2 | d | d | 2 |
| p3 | f | e | 3 |
| p4 | x | f | 1 |
| p5 | w | g | 2 |
| p6 | g | s | 3 |
| p7 | x | h |null|
| p8 | q | null | 4 |
| p9 | w | null |null|
预期产出:
具有相同C:[{p1,p4},{p2,p5},{p3,p6}]的成对行的列表
p1是从实体中检索到的hibernate对象,而不是字符串或列。p1是第一行的对象。我想得到一对hibernate对象,一对行。你的问题不清楚,请举例说明数据和需要的输出。@Ramki我编辑了我的文章。谢谢,您可以尝试LISTAGG,这将在单列搜索中提供多行值。例如,internet@Ramki p1中的hibernate对象是从实体中检索的,而不是字符串或列。p1是第一行的对象。我想得到一对hibernate对象,一对行。对不起……我想我的输入不清楚……我假设p1是从实体中检索到的hibernate对象,而不是字符串和列。p1是第一行的对象
with data (rn, person, a, b, c) as
(
select rownum rn, t.* from
(
select 'p1', 'w' , 'd' , 1 from dual union all
select 'p2', 'd' , 'd' , 2 from dual union all
select 'p3', 'f' , 'e' , 3 from dual union all
select 'p4', 'x' , 'f' , 1 from dual union all
select 'p5', 'w' , 'g' , 2 from dual union all
select 'p6', 'g' , 's' , 3 from dual union all
select 'p7', 'x' , 'h' , null from dual union all
select 'p8', 'q' , null , 4 from dual union all
select 'p9', 'w' , null , null from dual
) t
)
,
cte (rn, person, a, b, c, pair) as
(
select rn, person, a, b, c, null from data
union all
select data.rn, null, null, null, null, '{' || cte.person || ',' || data.person || '}' from cte join data on (cte.c = data.c and cte.rn < data.rn)
)
select
'"C":' ||
'[' || listagg(pair, ',') within group(order by rn) || ']' result
from cte
where pair is not null;