2015-03-08 85 views
-3

我需要的东西,如:SQL我怎样才能把一个选择到平均

选择AVG(选择不同的...) 从...

,但它不工作

来自备注:

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 
+2

你是什么意思'不起作用'?请详细说明并提供您的**完整代码**。另外,绝对'SQL'正确,***不***'MySQL'? – cybermonkey 2015-03-08 22:09:45

+0

标记为“太宽”。 – cybermonkey 2015-03-08 22:17:30

+0

sql..and其 选择 AVG( 从departmentx选择不同x.money X 内加入departmentyý 上x.identity = y.identity 工会 从departmentx X 内选择不同y.money 加入departmenty y on x.identity = y.identity where money not null ) from department – AmlesLausiv 2015-03-08 22:20:37

回答

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 

我已经离开了 “weirdisms”:

  • 你并不需要在子查询distinctunion照顾到这一点。
  • 你不需要money is not nullavg()忽略NULL
  • 您可能不应该命名列money,因为这是内置类型的名称。
相关问题