因为“学号 姓名 科目 成绩”与“学号 姓名 语文 数学 总分”列名不同,即使进行联合查询也不能合并出你需要的表
只能再建一个以拥有列名“语文 数学 总分”的新表,然后建个存储过程更新新表数据
我现在身边没有 数据库环境 这个是我以前写的SQL
你看一下,修改一下就可以了
Oracle分组查询用逗号分隔结果SQL语句
表一:
学号 姓名
1 张三
2 李四
3 王五
。。。。
表二:
学号 选修课程
1 语文
1 数学
2 英语
2 语文
3 数学
3 英语
3 历史
。。。。。
要求查处结果
学好 姓名 选修课程所有课程名称以,隔开
1 张三 语文,数学
2 李四 英语,语文
3 王五 数学,英语,历史
;
create table a_lyh_test
as
select 1 as "学号" , '张三' as "姓名" from dual
union all
select 2 as "学号" , '李四' as "姓名" from dual
union all
select 3 as "学号" , '王五' as "姓名" from dual
;
create table b_lyh_test
as
select 1 as "学号" , '语文' as "选修课程" from dual
union all
select 1 as "学号" , '数学' as "选修课程" from dual
union all
select 2 as "学号" , '英语' as "选修课程" from dual
union all
select 2 as "学号" , '语文' as "选修课程" from dual
union all
select 3 as "学号" , '数学' as "选修课程" from dual
union all
select 3 as "学号" , '英语' as "选修课程" from dual
union all
select 3 as "学号" , '历史' as "选修课程" from dual
;
select f"学号"
,f"姓名"
,ltrim(max(sys_connect_by_path(f"选修课程",','))
keep (dense_rank last order by fpnum),',') as "选修课程"
from
(
select t"学号"
,t"姓名"
,t"选修课程"
,row_number() over(partition by t"学号" order by t"姓名") as pnum
,row_number() over(partition by t"学号" order by t"姓名")-1 as lnum
from
(
select a"学号",a"姓名",b"选修课程"
from a_lyh_test a
,b_lyh_test b
where a"学号" = b"学号"
) t
) f
group by f"学号",f"姓名"
connect by flnum = prior fpnum and f"学号" = prior f"学号"
start with fpnum = 1;
使用union all合并查询结果,不过前提是必须保证每个sql查询结果格式一致。
如:
select a,b,c from c
union all
select e,f,g from b
>
创建表插入数据:
create table test(patient int,
visit_id int,
recording_date date,
value1 number(6,2),
value2 int,
value3 int);
insert into test values (1562074,1,to_date('2017-12-27','yyyy-mm-dd'),null,0,null);
insert into test values (1562074,1,to_date('2017-12-25','yyyy-mm-dd'),null,null,60);
insert into test values (1562074,1,to_date('2017-12-13','yyyy-mm-dd'),null,null,67);
insert into test values (1562074,1,to_date('2017-12-25','yyyy-mm-dd'),384,null,null);
insert into test values (1562074,1,to_date('2017-12-11','yyyy-mm-dd'),null,5,null);
insert into test values (1562074,1,to_date('2017-12-25','yyyy-mm-dd'),null,3,null);
insert into test values (1562074,1,to_date('2017-12-11','yyyy-mm-dd'),38,0,null);
commit;
运行:
select patient,visit_id,recording_date,sum(value1) value1,sum(value2) value2,sum(value3) value3 from test group by patient,visit_id,recording_date结果:
以上就是关于oracle 合并数据全部的内容,包括:oracle 合并数据、求助,oracle多行数据合并成一行、ORACLE三条SQL如何合并,求解等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)