2017-05-29 74 views
-3

数据库中的表具有列a,b,c。数据库中的每两行在列c中具有相同的值。我想要获取并存储这些对以用于下一个操作。 我正在使用休眠(但不是标准接口)。 什么是最佳解决方案?选择行的所有字段并根据一列对它们进行分组

人实体:

+--------------+----+-------+---+ 
| 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对象,一对行。

+1

你的问题还不清楚,举一些例子说明数据和需要输出什么。 – Ramki

+0

@Ramki我编辑了我的帖子。谢谢你的 – faraa

+1

你可以试试LISTAGG()这会给多个 行值int单列搜索网络中的一些例子https://www.techonthenet.com/oracle/functions/listagg.php – Ramki

回答

0
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; 
+0

对不起。我认为我的输入不清楚...我假设p1作为从实体检索到的hibernate对象而不是字符串和列。 p1是第一行的对象 – faraa

相关问题