查询以组合其中一列的两行

时间:2017-10-12 06:57:41

标签: sql oracle

在PL / SQL开发人员中,我想编写一个查询,为列提供合并的行值?恩。如果emp 1和2具有相同的工资,如200,而员工3,4,5的工资为1000,那么输出应该与1和2 emp相似,结果应该显示单个值200合并两行。对于emp 3,4和5,salary列应该只显示单个值1000,在salary列中合并3行。

如图所示

enter image description here

2 个答案:

答案 0 :(得分:0)

为了按AND AND分组并显示您可以使用的值:

create table vls(empid int, tval int);
insert into vls(empid, tval)
    values (1,200),(2,200),(3,1000),(4,1000),(5,1000);
select tval as grouped_value,sum(tval) as sum_tval,avg(tval) as avg_tval
from vls
group by tval;

答案 1 :(得分:0)

create table vls(empid int, tval int);

insert into vls(empid, tval) values (1,200);
insert into vls(empid, tval) values (2,200);
insert into vls(empid, tval) values (3,1000);
insert into vls(empid, tval) values (4,1000);
insert into vls(empid, tval) values (5,1000);

select listagg(empid, ' ') within group (order by empid) as empids, tval
from vls
group by
 tval;