sql如何将select放入avg中

时间:2015-03-08 22:07:53

标签: sql

我需要类似的东西:

选择平均值(选择不同的...) 从...

但它不起作用

来自评论:

select avg( select distinct x.money
            from departmentx x inner join
                 departmenty y
                 on x.identity = y.identity
            union
            select distinct y.money
            from departmentx x inner join
                 departmenty y
                 on x.identity = y.identity
            where money not null
          )
from department

1 个答案:

答案 0 :(得分:1)

以下可能是您编写的代码的意图:

select avg(money)
from (select distinct x.money
      from departmentx x inner join
           departmenty y
           on x.identity = y.identity
      union
      select distinct y.money
      from departmentx x inner join
           departmenty y
           on x.identity = y.identity
      where money is not null
     ) m

我离开了“古怪主义”:

  • 子查询中不需要distinctunion负责照顾。
  • 您不需要money is not nullavg()忽略NULL
  • 您可能不应该命名列money,因为这是内置类型的名称。