问题: Oracle 中怎么把两张行数不同的表合并成一个表? Sql 为: select s.stagename sname, sum(nworkhours) nworkhours, sum(O

问题:

Oracle 中怎么把两张行数不同的表合并成一个表?

Sql 为:

select s.stagename sname,

sum(nworkhours) nworkhours,

sum(OVERHOURS) OVERHOURS,

sum(nworkhours+OVERHOURS)

from T_WorkDetails w,

t_stage s

where w.stageid=s.stageid and w.projectid='D0927380468F4A4EE04010AC0C051F15'

group by s.stagename

ORDER by s.stagename

显示为:

【数据库】Oracle 中列添加和不同行数的两张表合并

第二个SQL 为:

select sum(total) as temptal from (select s.stagename sname,

sum(nworkhours) nworkhours,

sum(OVERHOURS) OVERHOURS,

sum(nworkhours+OVERHOURS) total

from T_WorkDetails w,

t_stage s

where w.stageid=s.stageid and w.projectid='D0927380468F4A4EE04010AC0C051F15'

group by s.stagename

ORDER by s.stagename)

显示为:

【数据库】Oracle 中列添加和不同行数的两张表合并

想要显示的结果为:

【数据库】Oracle 中列添加和不同行数的两张表合并

核心思想:

总体的sql如下:

select * from (select s.stagename sname,

sum(nworkhours) nworkhours,

sum(OVERHOURS) OVERHOURS,

sum(nworkhours+OVERHOURS)

from T_WorkDetails w,

t_stage s

where w.stageid=s.stageid and w.projectid='D0927380468F4A4EE04010AC0C051F15'

group by s.stagename

ORDER by s.stagename) t1,

(select sum(total) as temptal from (select s.stagename sname,

sum(nworkhours) nworkhours,

sum(OVERHOURS) OVERHOURS,

sum(nworkhours+OVERHOURS) total

from T_WorkDetails w,

t_stage s

where w.stageid=s.stageid and w.projectid='D0927380468F4A4EE04010AC0C051F15'

group by s.stagename

注意:行数少的那一张表会自动补齐行数和行数多的那一张表对应

【数据库】Oracle 中列添加和不同行数的两张表合并