当前位置:Gxlcms > mysql > Oracle中列添加和不同行数的两张表合并

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

时间:2021-07-01 10:21:17 帮助过:76人阅读

问题: 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

显示为:

第二个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)

显示为:

想要显示的结果为:

核心思想:

总体的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

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

linux

人气教程排行