日期:2014-05-17  浏览次数:20422 次

一个应用问题,请帮忙看看
select f.a,sum(xyCount)
from
(select f.a,count(distinct(ly.b)) xyCount
from f inner join ly on ...
group by f.a
union
select f.a,count(distinct z.b) xyCount
from f inner join z on ...
group by f.a
)
group by a

两个使用了聚合函数的结果及并集后再group by,这样写有错误
有什么好办法吗?
------最佳解决方案--------------------
select a,sum(xyCount)
 from
 (select f.a,count(distinct(ly.b)) xyCount
 from f inner join ly on ...
 group by f.a
 union
 select f.a,count(distinct z.b) xyCount
 from f inner join z on ...
 group by f.a
 ) as TB
 group by a

------其他解决方案--------------------
select f.a,sum(xyCount)
 from
 (select f.a,count(distinct(ly.b)) xyCount
 from f inner join ly on ...
 group by f.a
 union
 select f.a,count(distinct z.b) xyCount
 from f inner join z on ...
 group by f.a
 ) a
 group by a
加个红字
------其他解决方案--------------------
select f.a,sum(xyCount) xyCount
 from
 (select f.a,count(distinct(ly.b)) xyCount
 from f inner join ly on ...
 group by f.a
 union
 select f.a,count(distinct z.b) xyCount
 from f inner join z on ...
 group by f.a
 ) as B 
 group by a
绿色的地方去掉,红色的地方加上
------其他解决方案--------------------
“绿色的地方去掉,红色的地方加上 ”。

谢谢!
------其他解决方案--------------------
派生表需要有一个名称,否则会报错