尝试在一个表中列出两年的成本错误无法获得结果

时间:2018-02-13 17:32:34

标签: sql oracle

我希望按代理商名称获得2016年和2017年的平均成本组。但我无法得到结果,我不确定错误。谢谢你的帮助。

表格是

 Agencyname  Agencyid   Timesheetcost   projectyear
   companya  1          500             2016
   companya  1          600             2016
   companyb  2          700             2017
   companyb  2          800             201[7][1]

输出想要

   agencyname   agencyid    2016_averagecost    2017_averagecost    
   companya     1           500                 600 
   companyb     2          700                  800 

查询是:

select a.AGENCYNAME,a.projectyear,avg(a.timesheetcost),b.AGENCYNAME, b.projectyear,avg(b.timesheetcost)from table a, table b
where a.PROJECTYEAR = 2017 or b.PROJECTYEAR=2016;
group by a.AGENCYNAME,b.AGENCYNAME;

1 个答案:

答案 0 :(得分:0)

您可以使用相关子查询执行所需操作。假设您的表被称为"费用",您可以这样做:

select agencyname,  
     (select avg(c.timesheetcost) from costs c where a.agencyid=c.agencyid and c.projectyear=2016) as 2016_averagecost, 
     (select avg(d.timesheetcost) from costs d where a.agencyid=d.agencyid and d.projectyear=2017) as 2017_averagecost
from costs a